datagridviewcombobox 列 依赖于另一列

本文关键字:一列 依赖于 datagridviewcombobox | 更新日期: 2023-09-27 18:30:47

如何在网格视图组合框列上执行此操作 任何想法 plx

表单加载事件

string query="select article_name from article";
SqlCommmand cmd = new SqlCommand(query,con);
SqlDataAdapter da= new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
combobox1.items.clear();
for(int i=0;i<dt.rows.count;i++)
{
 combobox1.items.add(dt.rows[i].cells[0].toString());
}

''ComboBox1 选定的索引更改事件

string query1="select description from article where article_name='"+combobox1.selectedItem.ToString()+"'";
SqlCommmand cmd1 = new SqlCommand(query1,con);
SqlDataAdapter da1= new SqlDataAdapter(cmd);
DataTable dt1=new DataTable();
da1.Fill(dt1);
combobox2.items.clear();
for(int i=0;i<dt1.rows.count;i++)
{
 combobox2.items.add(dt1.rows[i].cells[0].toString());
}

''现在假设这 2 个组合框是网格视图组合框列,那么如何制作 这项工作在网格视图组合框列上

Project in Windows Form in C#

datagridviewcombobox 列 依赖于另一列

几个月后我发布此answer,因为它有助于 嗖嗖嗖地面临DataGridviewComboboxcell问题 我做了自己的技能 首先填写我的第一个/主列

SqlCommand objCmd = new SqlCommand("select distinct article_name from Setup_article_custominvoice", con);
                    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
                    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
                    DataTable dt = new DataTable();
                    objDA.Fill(dt);
                    article.DataSource = dt;
                    //this column1 will display as text
                    article.DisplayMember = "article_name";

在那之后,我继续Cell End Edit

if (dataGridView1.CurrentCell == dataGridView1.CurrentRow.Cells["article_name"])
                {
                    string CategoryValue = "";
                    //string CategoryValue1 = "";
                    if (dataGridView1.CurrentCell.Value != null)
                    {
                        CategoryValue = dataGridView1.CurrentCell.Value.ToString();
                        //CategoryValue1 = dataGridView1.CurrentCell.Value.ToString();
                    }
                    //SqlConnection objCon = new SqlConnection(@"Data Source=.'SqlExpress;Initial Catalog=dbTest3;Integrated Security=True");
                    string query = "select article_name,composition from Setup_article_custominvoice where article_name='" + CategoryValue + "'";
                    SqlCommand objCmd = new SqlCommand(query, con);
                    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
                    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
                    DataTable dt = new DataTable();
                    objDA.Fill(dt);
                    DataGridViewComboBoxCell t = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2] as DataGridViewComboBoxCell;
                    t.DataSource = dt;
                    t.DisplayMember = "composition";
                }