c# - 根据另一个组合框中的选择更改 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);
}
}
}
}
解决方案
推荐阅读
- javascript - 显示警报在调用 ajax 时未找到日期之间的数据
- node.js - 如何显示登录用户引用的所有帖子?
- powershell - 有没有办法为 Powershell 版本 5 的 InvokeRest 方法获取响应标头
- python - 查询时格式化查询结果
- javascript - 如何使用javascript在地图中显示两个制造商之间的路线
- javascript - 未捕获的 SyntaxError:使用提交处理程序的意外标识符
- sql-server - 为什么从 VB.net 应用程序执行存储过程比从 SSMS 执行存储过程需要更长的时间?
- python-3.x - 删除 setup.py 中的 Cython 断言选项
- java - 在 AtomicInteger.incrementAndGet() 的 CompareAndSwap 中比较比较什么?
- jquery - 如何使用 jQuery 将日期自动更改为 1 个月