c# - 需要为动态创建的 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);
}
}
}
}
解决方案
推荐阅读
- composer-php - 精确版本的 TYPO3 的作曲家设置
- firebase - 如何编写firestore规则来查询另一个集合,其中值等于某个值
- javascript - 使用一行中的数据在 ag-grid (Vue.js) 中创建自定义聚合函数
- openapi - OpenAPI 中正则表达式“模式”的共同点是什么?
- java - Spring Boot 属性解析不正确
- c++ - Check for end-of-list in boost::intrusive::list without container?
- javascript - 如何抓取网页内容需要点击按钮
- c# - ASP.NET 3.5 使用 System.Drawing.Printing PrintDocument 打印现有 PDF 文件
- python - 如何在 Django 中使用 __init__ 预填充模型表单字段中的值?
- c# - 通过 join 将 c# linq 转换为 sql 代码并进行优化