How to Search Data in the DataGridView Using a ComboBox in C#

Searching data is very important most especially if you have a lot of data stored in the database. In this way, it will be easier for you to search the data that you need. So, in this tutorial, I will teach you how to search data in the datagridview using a combobox in c# and MySQL database. I based this on my last tutorial which is How to Fill Data in a ComboBox Using C# and MySQL Database because of their similar method. Let's get started.

Creating Database

Create a Database named “peopledb”. After that, execute the following query below for creating and adding the data in the table.
  1. CREATE TABLE `tbluser` (
  2. `UserId` int(11) NOT NULL,
  3. `Fullname` varchar(124) NOT NULL
  4.  
  5. --
  6. -- Dumping data for table `tbluser`
  7. --
  8.  
  9. INSERT INTO `tbluser` (`UserId`, `Fullname`) VALUES
  10. (3, 'Janno Palacios'),
  11. (4, 'Craig'),
  12. (7, 'cherry lou velez'),
  13. (8, 'velez lou'),
  14. (9, 'jom');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#. c#combo21

Step 2

Do the Form just like this. C#Combo12323421

Step 3

Add mysql.data.dll for your references.

Step 4

Go to the code editor and add using MySql.Data.MySqlClient; above the namespace to access MySQL library;

Step 5

Initialize the connection between mysql and c#. After that, declare all the classes and variables that are needed.
  1. MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=janobe;database=peopledb;sslMode=none");
  2. MySqlCommand cmd;
  3. MySqlDataAdapter da;
  4. DataTable dt;
  5. string sql;

Step 6

Create a method for filling the data in the combobox.
  1. private void LoadCombo(string sql,string DisplayMember,string ValueMember)
  2. {
  3. try
  4. {
  5. con.Open();
  6. cmd = new MySqlCommand();
  7. cmd.Connection = con;
  8. cmd.CommandText = sql;
  9. da = new MySqlDataAdapter();
  10. da.SelectCommand = cmd;
  11. dt = new DataTable();
  12. da.Fill(dt);
  13.  
  14. comboBox1.DataSource = dt;
  15. comboBox1.DisplayMember = DisplayMember;
  16. comboBox1.ValueMember = ValueMember;
  17.  
  18.  
  19.  
  20. }
  21. catch(Exception ex)
  22. {
  23. MessageBox.Show(ex.Message);
  24.  
  25. }finally
  26. {
  27. con.Close();
  28.  
  29. }
  30. }

Step 7

Create a method to display data in the datagridview.
  1. private void LoadGrid(string sql, DataGridView dtg)
  2. {
  3. try
  4. {
  5. con.Open();
  6. cmd = new MySqlCommand();
  7. cmd.Connection = con;
  8. cmd.CommandText = sql;
  9. da = new MySqlDataAdapter();
  10. da.SelectCommand = cmd;
  11. dt = new DataTable();
  12. da.Fill(dt);
  13. dtg.DataSource = dt;
  14. }
  15. catch (Exception ex)
  16. {
  17. MessageBox.Show(ex.Message);
  18. }
  19. finally
  20. {
  21. con.Close();
  22. }
  23. }

Step 8

Write this code for displaying data in the combobox and datagridview in the first load of the form.
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3. sql = "SELECT * FROM `tbluser`";
  4. LoadCombo(sql, "Fullname", "UserId");
  5.  
  6. sql = "SELECT * FROM `tbluser`";
  7. LoadGrid (sql,dataGridView1 );
  8. }

Step 9

Write the following codes for searching data using combobox in the datagridview.
  1. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. sql = "SELECT * FROM `tbluser` WHERE UserId='" + comboBox1.SelectedValue + "'";
  4. LoadGrid(sql, dataGridView1);
  5. }
For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment