如何在WinForm中执行dataGridViewcomboBoxColumn的selectedIndexChanged事件 [英] How to perform selectedIndexChanged event of dataGridViewcomboBoxColumn in WinForm

查看:95
本文介绍了如何在WinForm中执行dataGridViewcomboBoxColumn的selectedIndexChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WinForm应用程序中,我使用了 dataGridView1 控件。在这个 dataGridView1 中,我添加了三个 dataGridViewcomboBoxColumn ,即: ProductCode SelectCategory SelectCategory 。每当用户从 ProductCode dataGridViewcomboBoxColumn 中选择类别时,应该从 SelectCategory ProductName 中的SQL Server 2005数据库中自动检索数据。 b> dataGridViewcomboBoxColumn 。我是这个领域的新人。请帮帮我。

我为它写了一些代码:但它不起作用



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

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());
           }

       }

推荐答案

读取这篇文章。您将明确了解如何操作。
Read this Article. You'll get a clear concept of how to do.


这篇关于如何在WinForm中执行dataGridViewcomboBoxColumn的selectedIndexChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆