Home > DeveloperSection > Articles > See The Whole Data in DataGridView With Selecting A Value From ComboBox:

See The Whole Data in DataGridView With Selecting A Value From ComboBox:


C# C# 
Ratings:
1 Comment(s)
 6099  View(s)
Rate this:

See The Whole Data in DataGridView With Selecting A Value From ComboBox:

In this article I will give an example that how to display the data in the datagridview on the basis of combobox value.

*Write this code on the click event of Button*

private void button1_Click(object sender, EventArgs e)
{
            SqlConnection con = new SqlConnection();
//create a SqlConnection class object
            con.ConnectionString = "Data Source=home-1117080bc2;Initial Catalog=Hotel_Booking;User  
            ID=sa;Password=sqlforarijit";
//create a connecting string
            con.Open();
//open the connection
            SqlCommand cmd = new SqlCommand("select * from new_reservation where customer_id =
           @a", con);  
//retrieve record(s) of customer from the database on the basis of id
            cmd.Parameters.Add("@a", SqlDbType.VarChar, 50);
//add a parameter
            cmd.Parameters["@a"].Value = comboBox1.Text;
//pass the parameter value from combobox
            SqlDataAdapter ada = new SqlDataAdapter(cmd);
//create a SqlDataAdapter object and pass
                                                                                                        
SqlCommand object as a parameter
            SqlCommandBuilder build = new SqlCommandBuilder(ada);
// create a SqlCommandBuilder
                                                                                
object and pass SqlDataAdapter object as a parameter
            DataSet ds = new DataSet();
//create a dataset
            ada.Fill(ds, "new_reservation");
//fill the data in a dataset, pass dataset object and virtual  
                                                         table name

            dataGridView1.DataSource = ds.Tables[0];
// set dataset table as a datasource of datagridview
   }

After performing the above task build and run your project. Now when you click on the button it display the data in datagridview on the basis of combobox value. The select query fetches the record of that customer which customer id has been selected in the combobox.

Thanks.


good

By aken H on   5 years ago

Hi Arijit Bhadra,

nice article.

thanks.


Don't want to miss updates? Please click the below button!

Follow MindStick