首页 > 解决方案 > 根据另一个组合框中的选择更改 datagridview 中的组合框

问题描述

我有一个组合框,它为 DataGridView 中的下一个组合框设置数据集。第一个问题是获取数据,我正在使用 CellValueChanged 事件。我不确定这是否是最好的方法,但它确实有效。

        private void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;
            if (e.ColumnIndex == dataGridViewLocalReceipts.Columns[1].Index && e.RowIndex >= 0)
            {
                string Person = (string)dataGridViewLocalReceipts.Rows[e.RowIndex].Cells[1].Value;  //='Staff'

在这一点上,我需要一些东西来清除数据集,以便任何时候[单元格值更改],我都可以重置。目前,没有错误,但第二个组合框没有变化。


                //insert data to clear combobox
                
                if (Person == "Client")
                {
                    try
                    {

                        DataSet ds = new DataSet();
                        DataTable dt;
                        dt = new DataTable();

                        string queryClients = null;
                        SqlDataAdapter daClients = null;
                        DataSet dsClients = null;
                        
                        queryClients = "SELECT CLIENT, CLIENT_NUMBER  FROM xxTable WHERE ORGANIZATION = '" + Program.GlobalVariables.GlobalProgram + "' ORDER BY 1";
                        daClients = new SqlDataAdapter(queryClients, conRT);
                        conRT.Open();
                        dsClients = new DataSet();
                        daClients.Fill(dsClients, "CLIENT");

                        DataGridViewComboBoxCell cell;
                        cell = dataGridViewLocalReceipts.Rows[e.RowIndex].Cells[dataGridViewLocalReceipts.Columns["Person"].Index] as DataGridViewComboBoxCell;
                        cell.DisplayMember = "CLIENT";
                        cell.ValueMember = "PERSONID";
                        cell.DataSource = dsClients.Tables["CLIENT"];

                        conRT.Close();
                    }
                    catch (Exception ex)
                    {
                        conRT.Close();
                        MessageBox.Show("Error occured while loading the Client List! " + ex);
                    }
                }
                else if (Person == "Staff")
                {
                    try
                    {
                        string queryStaff = null;
                        SqlDataAdapter daClients = null;
                        DataSet dsStaff = null;                        
                        daClients = new SqlDataAdapter(queryStaff, conRT);
                        
                        conRT.Open();
                        queryStaff = "SELECT STAFF,PERSONID FROM xxTable ORDER BY 1";
                        dsStaff = new DataSet();
                        daClients.Fill(dsStaff, "STAFF");

                        DataGridViewComboBoxCell cell = dataGridViewLocalReceipts.Rows[e.RowIndex].Cells[dataGridViewLocalReceipts.Columns["Person"].Index] as DataGridViewComboBoxCell;
                        cell.DisplayMember = "STAFF";
                        cell.ValueMember = "PERSONID";
                        cell.DataSource = dsStaff.Tables["STAFF"];
                        daClients.Dispose();
                        conRT.Close();
                    }
                    catch (Exception ex)
                    {
                        conRT.Close();
                        MessageBox.Show("Error occured while loading the Staff List! " + ex);
                    }
                }
            }
        }

标签: c#datagridviewcombobox

解决方案


推荐阅读