Filter Combobox Datasource In C#

This article is about Filter Combobox Datasource in C#. C# controls are located in the Toolbox of the development environment, and you use them to create objects on a form with a simple series of mouse clicks and dragging motions. Filter combobox using C# can display a text box combined with a ListBox, which enables the user to select items from the list or enter a new value. In this tutorial, you will learn in detail how to filter data that will display to atextbox and datagridview. We can use Event Combobox SelectedIndexChanged to complete this tutorial.
 

Combobox SelectedIndexChanged event

 
The SelectedIndexChanged  event of a combobox fires when you change the selected item in a combobox. If you want to do something when you change the selection, you can write the program on the SelectedIndexChanged  event. From the following code you can understand how to set values in the SelectedIndexChanged  event of a combobox.
 
This tutorial uses Mysql for connecting the database and binding data. Follow this step to see how filtering data will display to Textbox and Datagridview. 
  1. Create a new application project. In Visual Studio, on the menu click File> New  > Project. For more details, see the following menu on the display.

  1. Then the New Project window will appear. 

  1. Write down the name of the project that will be created on a field name. Specify the directory storage project by accessing the field location. Next, give the name of the solution in the Solution Name. Then click OK.
Create a new class for connecting database to Mysql and write the following program listing:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using MySql.Data.MySqlClient;  
  6. using System.Windows.Forms;  
  7. using System.Data;  
  8.   
  9. namespace Filter_Combobox  
  10. {  
  11.     class ConnectionDB  
  12.     {  
  13.   
  14.         MySql.Data.MySqlClient.MySqlConnection conn;  
  15.         string myConnectionString;  
  16.         static string host = "localhost";  
  17.         static string database = "test";  
  18.         static string userDB = "ecco";  
  19.         static string password = "password";  
  20.         public static string strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;  
  21.   
  22.   
  23.         public bool Open()  
  24.         {  
  25.             try  
  26.             {  
  27.                 strProvider = "server=" + host + ";Database=" + database + ";User ID=" + userDB + ";Password=" + password;  
  28.                 conn = new MySqlConnection(strProvider);  
  29.                 conn.Open();  
  30.                 return true;  
  31.             }  
  32.             catch (Exception er)  
  33.             {  
  34.                 MessageBox.Show("Connection Error ! " + er.Message, "Information");  
  35.             }  
  36.             return false;  
  37.         }  
  38.   
  39.         public void Close()  
  40.         {  
  41.             conn.Close();  
  42.             conn.Dispose();  
  43.         }  
  44.   
  45.         public DataSet ExecuteDataSet(string sql)  
  46.         {  
  47.             try  
  48.             {  
  49.                 DataSet ds = new DataSet();  
  50.                 MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);  
  51.                 da.Fill(ds, "result");  
  52.                 return ds;  
  53.             }  
  54.             catch (Exception ex)  
  55.             {  
  56.                 MessageBox.Show(ex.Message);  
  57.             }  
  58.             return null;  
  59.         }  
  60.   
  61.         public MySqlDataReader ExecuteReader(string sql)  
  62.         {  
  63.             try  
  64.             {  
  65.                 MySqlDataReader reader;  
  66.                 MySqlCommand cmd = new MySqlCommand(sql, conn);  
  67.                 reader = cmd.ExecuteReader();  
  68.                 return reader;  
  69.             }  
  70.             catch (Exception ex)  
  71.             {  
  72.                 MessageBox.Show(ex.Message);  
  73.             }  
  74.             return null;  
  75.         }  
  76.   
  77.         public int ExecuteNonQuery(string sql)  
  78.         {  
  79.             try  
  80.             {  
  81.                 int affected;  
  82.                 MySqlTransaction mytransaction = conn.BeginTransaction();  
  83.                 MySqlCommand cmd = conn.CreateCommand();  
  84.                 cmd.CommandText = sql;  
  85.                 affected = cmd.ExecuteNonQuery();  
  86.                 mytransaction.Commit();  
  87.                 return affected;  
  88.             }  
  89.             catch (Exception ex)  
  90.             {  
  91.                 MessageBox.Show(ex.Message);  
  92.             }  
  93.             return -1;  
  94.         }  
  95.     }  


  1. Create textbox, combobox, label properties in a windows form like below.

  1. Create Datagridview with properties like below.

Next step, BGo back to Windows form and view the code to write the following program listing,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using MySql.Data.MySqlClient;  
  10. using System.Data;  
  11. using System.Collections;  
  12.   
  13. namespace Filter_Combobox  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         ConnectionDB con = new ConnectionDB();  
  18.   
  19.         //arraylist to getter and setter data  
  20.         private static ArrayList ListID = new ArrayList();  
  21.         private static ArrayList ListFirstname = new ArrayList();  
  22.         private static ArrayList ListLastname = new ArrayList();  
  23.         private static ArrayList ListTelephone = new ArrayList();  
  24.         private static ArrayList ListAddress = new ArrayList();  
  25.   
  26.         public Form1()  
  27.         {  
  28.             InitializeComponent();  
  29.         }  
  30.   
  31.         private void getDataComboBox()  
  32.         {  
  33.             con.Open();  
  34.             string query = "select firstname from user";  
  35.             MySqlDataReader row;  
  36.             row = con.ExecuteReader(query);  
  37.             if (row.HasRows)  
  38.             {  
  39.                 while (row.Read())  
  40.                 {  
  41.                     comboBox1.Items.Add(row["firstname"].ToString());  
  42.                 }  
  43.   
  44.             }  
  45.             con.Close();  
  46.         }  
  47.   
  48.         private void Form1_Load(object sender, EventArgs e)  
  49.         {  
  50.             getDataComboBox();  
  51.             GetData();  
  52.             if (ListID.Count > 0)  
  53.             {  
  54.                 updateDatagrid();  
  55.             }  
  56.         }  
  57.   
  58.         private void GetData()  
  59.         {  
  60.             try  
  61.             {  
  62.                 con.Open();  
  63.                 string query = "select id,firstname,lastname,telephone,address from user";  
  64.   
  65.                 //MySqlDataReader row;  
  66.                 MySqlDataReader row;  
  67.                 row = con.ExecuteReader(query);  
  68.                 if (row.HasRows)  
  69.                 {  
  70.                     while (row.Read())  
  71.                     {  
  72.                         ListID.Add(row["id"].ToString());  
  73.                         ListFirstname.Add(row["firstname"].ToString());  
  74.                         ListLastname.Add(row["lastname"].ToString());  
  75.                         ListTelephone.Add(row["telephone"].ToString());  
  76.                         ListAddress.Add(row["address"].ToString());  
  77.                     }  
  78.                 }  
  79.                 else  
  80.                 {  
  81.                     MessageBox.Show("Data not found");  
  82.                 }  
  83.   
  84.                 con.Close();  
  85.             }  
  86.             catch (Exception err)  
  87.             {  
  88.                 MessageBox.Show(err.ToString());  
  89.             }  
  90.   
  91.         }  
  92.   
  93.         private void updateDatagrid()  
  94.         {  
  95.             dataGridView1.Rows.Clear();  
  96.             for (int i = 0; i < ListID.Count; i++)  
  97.             {  
  98.                 DataGridViewRow newRow = new DataGridViewRow();  
  99.   
  100.                 newRow.CreateCells(dataGridView1);  
  101.                 newRow.Cells[0].Value = ListID[i];  
  102.                 newRow.Cells[1].Value = ListFirstname[i];  
  103.                 newRow.Cells[2].Value = ListLastname[i];  
  104.                 newRow.Cells[3].Value = ListTelephone[i];  
  105.                 newRow.Cells[4].Value = ListAddress[i];  
  106.                 dataGridView1.Rows.Add(newRow);  
  107.             }  
  108.         }  
  109.   
  110.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
  111.         {  
  112.             try  
  113.             {  
  114.                 if (comboBox1.SelectedItem.ToString() == "ALL")  
  115.                 {  
  116.                     clearData();  
  117.                     GetData();  
  118.                     if (ListID.Count > 0)  
  119.                     {  
  120.                         updateDatagrid();  
  121.                     }  
  122.                 }  
  123.                 else  
  124.                 {  
  125.                     clearData();  
  126.                     con.Open();  
  127.                     string query = "select id,firstname,lastname,telephone,address from user where firstname = '" + comboBox1.SelectedItem.ToString() + "'";  
  128.   
  129.                     //MySqlDataReader row;  
  130.                     MySqlDataReader row;  
  131.                     row = con.ExecuteReader(query);  
  132.                     if (row.HasRows)  
  133.                     {  
  134.                         while (row.Read())  
  135.                         {  
  136.                             ListID.Add(row["id"].ToString());  
  137.                             ListFirstname.Add(row["firstname"].ToString());  
  138.                             ListLastname.Add(row["lastname"].ToString());  
  139.                             ListTelephone.Add(row["telephone"].ToString());  
  140.                             ListAddress.Add(row["address"].ToString());  
  141.                         }  
  142.   
  143.                         textBox2.Text = row["telephone"].ToString();  
  144.                         textBox3.Text = row["address"].ToString();  
  145.                         updateDatagrid();  
  146.                     }  
  147.                     else  
  148.                     {  
  149.                         MessageBox.Show("Data not found");  
  150.                     }  
  151.   
  152.                     con.Close();  
  153.                 }  
  154.             }  
  155.             catch (Exception err)  
  156.             {  
  157.                 MessageBox.Show(err.ToString());  
  158.             }  
  159.         }  
  160.   
  161.         private void clearData()  
  162.         {  
  163.             ListID.Clear();  
  164.             ListFirstname.Clear();  
  165.             ListLastname.Clear();  
  166.             ListAddress.Clear();  
  167.             ListTelephone.Clear();  
  168.             textBox2.Text = "";  
  169.             textBox3.Text = "";  
  170.         }  
  171.     }  


  1. After you write down the program listings, press the F5 key to run the program and if you successfully connect your database the result is:

We have explained how to make a Filter Combobox Datasource program in  C#, for those of you who want to download the source code of the program you can also do that.  Hopefully this discussion is helpful to you.
 
You can see Filter Combobox Datasource C# from Github project in Here.
 
Thank you for reading this article about Filter Combobox Datasource C#, I hope this article is useful for you. Visit My Github about .Net Csharp in Here


Similar Articles