首页 > 解决方案 > 需要为动态创建的 datagridview 组合框列清除数据源

问题描述

创建数据网格视图:

try
            {
                if (dataGridViewLocalReceipts.Rows.Count >= 0) { dataGridViewLocalReceipts.Rows.Clear(); }
                if (dataGridViewLocalReceipts.Columns.Count >= 0) { dataGridViewLocalReceipts.Columns.Clear(); }
                dataGridViewLocalReceipts.DataSource = null;
                dataGridViewLocalReceipts.Refresh();
                
                //Add column for Staff or Client
                DataGridViewComboBoxColumn ClientOrStaff = new DataGridViewComboBoxColumn();     
                ClientOrStaff.HeaderText = "Client or Staff?"; //adjust to variable for Client or Staff
                ClientOrStaff.Name = "ClientOrStaff";
                ClientOrStaff.Items.Add("Client");
                ClientOrStaff.Items.Add("Staff");
                ClientOrStaff.Width = receiptDateColumnLength;

                //Add column for staff/client list
                DataGridViewComboBoxColumn Person = new DataGridViewComboBoxColumn();     //dropdown for category of receipt
                Person.HeaderText = "Person"; //adjust to variable for Client or Staff
                Person.Name = "Person";
                Person.Items.Add(ClientOrStaff.DisplayMember);
                Person.Width = categoryPersonLength;
                
                dataGridViewLocalReceipts.Columns.Add(ClientOrStaff);
                dataGridViewLocalReceipts.Columns.Add(Person);
                dataGridViewLocalReceipts.EnableHeadersVisualStyles = false;
                string[] files = Directory.GetFiles(filePath);              //directory of saved scanned receipts
                for (int i = 0; i < files.Length; i++)
                {
                    FileInfo file = new FileInfo(files[i]);
                    var index = dataGridViewLocalReceipts.Rows.Add();
                    dataGridViewLocalReceipts.Rows[index].Cells["FileName"].Value = file.Name;
                };
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message);
            }
        }

然后我根据第一个组合框的选择填充第二个组合框。根据选择,我填充了一个数据集。问题是清除此设置,以便更改您的选择允许您重置。

private void DataGridViewLocalReceipts_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'

在这里,我假设可以获得一些东西来清除数据集和/或表,但我发现的所有功能都不能解决问题......

                if (Person == "Client")
                {
                    try
                    {
                        DataSet ds = new DataSet();
                        DataTable dt;
                        dt = new DataTable();

                        string queryClients = null;
                        SqlDataAdapter daClients = null;
                        DataSet dsClients = null;
                        
                        queryClients = "SELECT C, CN FROM Client 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,PERSON FROM Employees 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 = "PERSON";
                        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#sqldatagridview

解决方案


推荐阅读