我想在两列之间创建一个依赖关系 每行 的DataGridView中。我在运行时添加了两列(品牌,模型)(从数据库中选择数据)。我的目标是当品牌单元格发生变化时,在models单元格中加载新项目。填充数据。
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());
}
当我试图清除单元格中的项目时,出现了一个错误,为什么会出现这个错误?为什么会出现这样的错误呢?有什么其他的方法可以解决这个问题吗?
我知道这是个老问题,但还是要回答一下,也许对下一个问题有帮助。
我刚刚也遇到了同样的问题,稍微调试了一下,发现问题出在对象的值变量上.值变量保存着combobox的当前选择值,但是当你清除项目时,那么这个值就会变得无效,这就导致了错误.在清除之前将值变量设置为null就可以消除错误。