替换项目DataGridViewComboBoxCell c#
问题描述:
我想创建DataGridView中每行的两列之间的依赖关系。 我添加了两列(品牌,型号)运行时(从数据库中选择数据)。我的目标是在品牌单元格更改时在模型单元格中加载新项目。 填充数据:替换项目DataGridViewComboBoxCell c#
private void autoOptionsForm_Load(object sender, EventArgs e)
{
Connection.ds.Tables.Clear();
Connection.adap = new SqlDataAdapter("SELECT * FROM autoOptions", Connection.conn);
Connection.adap.Fill(Connection.ds, "autoOptions");
aoGV.DataSource = Connection.ds.Tables[0];
// Additing ComboBox column
DataGridViewComboBoxColumn carCB = new DataGridViewComboBoxColumn();
carCB.Name = "Model";
aoGV.Columns.Add(carCB);
DataGridViewComboBoxColumn brandCB = new DataGridViewComboBoxColumn();
brandCB.Name = "Brand";
aoGV.Columns.Add(brandCB);
foreach (DataGridViewRow row in aoGV.Rows) {
if (row.Index == aoGV.Rows.Count - 1) continue;
//Additing CB for Models
SqlCommand cmd = new SqlCommand(@"SELECT Model FROM Cars WHERE
Brand_ID = (SELECT Brand_ID FROM Cars WHERE id='" + row.Cells["Car_ID"].Value + "')", Connection.conn);
SqlDataReader sqlReader = cmd.ExecuteReader();
while (sqlReader.Read()) ((DataGridViewComboBoxCell)row.Cells["Model"]).Items.Add(sqlReader["Model"].ToString());
//Current Model ComboBox
cmd = new SqlCommand(@"SELECT Model FROM Cars WHERE id = "+ row.Cells["Car_ID"].Value, Connection.conn);
sqlReader = cmd.ExecuteReader();
sqlReader.Read();
int iId = ((DataGridViewComboBoxCell)row.Cells["Model"]).Items.IndexOf(sqlReader["Model"].ToString());
((DataGridViewComboBoxCell)row.Cells["Model"]).Value = ((DataGridViewComboBoxCell)row.Cells["Model"]).Items[iId];
//Additing CB for Brands
cmd = new SqlCommand(@"SELECT Brand_name FROM Brands", Connection.conn);
sqlReader = cmd.ExecuteReader();
while (sqlReader.Read()) ((DataGridViewComboBoxCell)row.Cells["Brand"]).Items.Add(sqlReader["Brand_name"].ToString());
cmd = new SqlCommand(@"SELECT Brand_name FROM Brands WHERE id =
(SELECT Brand_ID FROM Cars WHERE id = " + row.Cells["Car_ID"].Value + ")", Connection.conn);
sqlReader = cmd.ExecuteReader();
sqlReader.Read();
iId = ((DataGridViewComboBoxCell)row.Cells["Brand"]).Items.IndexOf(sqlReader["Brand_name"].ToString());
((DataGridViewComboBoxCell)row.Cells["Brand"]).Value = ((DataGridViewComboBoxCell)row.Cells["Brand"]).Items[iId];
}
}
我的方式来解决代码:
private void aoGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (aoGV.Columns[aoGV.SelectedCells[0].ColumnIndex].Name != "Brand") return;
DataGridViewRow row = aoGV.Rows[aoGV.SelectedCells[0].RowIndex];
SqlCommand cmd = new SqlCommand(@"SELECT Model FROM Cars WHERE
Brand_ID = (SELECT id FROM Brands WHERE Brand_name = '"+row.Cells["Brand"].Value.ToString()+"')", Connection.conn);
SqlDataReader sqlReader = cmd.ExecuteReader();
((DataGridViewComboBoxCell)row.Cells["Model"]).Items.Clear();
while (sqlReader.Read()) ((DataGridViewComboBoxCell)row.Cells["Model"]).Items.Add(sqlReader["Model"].ToString());
}
,并试图清除细胞的物品时,我有一个错误。为什么会出现此错误?(添加列在运行时手动执行)。是否有其他解决方案来解决问题?
答
我知道这是一个老问题,但仍然是一个答案可能有助于下一个。
我只是有同样的问题,一点点的调试表明,问题是对象的值变量。 值变量保存组合框当前选定的值,但是当您清除项目时,此值变为无效并导致错误。 在清除之前将value变量设置为null删除错误。
什么是错误? –
DataGridViewComboBoxCell值无效。 屏幕截图:http://joxi.ru/eAOlv7zFl6GYro –
处理DataError事件解决了该问题。但我认为这不是一个好的解决方案,因为我们正在逃避问题,但不解决问题。 –