Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my WinForm Application, I used a dataGridView1 control. In this dataGridView1 I added three dataGridViewcomboBoxColumn namely: ProductCode, SelectCategory and SelectCategory. Whenever the user will select category from ProductCode dataGridViewcomboBoxColumn, data should be automatically retrieved from SQL Server 2005 database in SelectCategory and ProductName dataGridViewcomboBoxColumn. I am very new in this field.Please help me.
I wrote some code for it: but it not working

C#
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
       {
           if (dataGridView1.CurrentCell.ColumnIndex == 2)
           {
               ComboBox comboBox = e.Control as ComboBox;
               if (comboBox != null)
               {
                   comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelecteIndexChanged);
               }

           }
       }
       void comboBox_SelecteIndexChanged(object sender, EventArgs e)
       {
           try
           {
               DataSet ds = new DataSet();
               string str = "select ProductCategory,ProductName from Product_Details where ProductID='" + dataGridView1.Rows[intRownum].Cells[2].Value + "'";
               SqlConnection con = new SqlConnection(Class1.cs);
               con.Open();
               SqlCommand cmd = new SqlCommand(str, con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(ds, "Product_Details");
               if (ds.Tables["Product_Details"].Rows.Count > 0)
               {
                   dataGridView1.Rows[intRownum].Cells[3].Value = ds.Tables["Product_Details"].Rows[intRownum][0].ToString();
                   dataGridView1.Rows[intRownum].Cells[4].Value = ds.Tables["Product_Details"].Rows[intRownum][1].ToString();
                   dataGridView1.Update();
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString());
           }

       }
Posted
Updated 9-Dec-13 19:19pm
v9

1 solution

Read this Article. You'll get a clear concept of how to do.
 
Share this answer
 
v3
Comments
Md M Ahmad 2-Dec-13 23:41pm    
Mr. Debopam Pal if you can't provide a proper solution on somebody's question, then please don't joke with someone by posting a unnecessary link.
Debopam Pal 2-Dec-13 23:53pm    
I'm extremely sorry...The link that I was posted somehow changed. Now Check. Is it OK?
Md M Ahmad 2-Dec-13 23:59pm    
Thanks
But not much useful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900