替换项DataGridViewComboBoxCell c#

时间:2015-05-09 18:06:07

标签: c# clear items datagridviewcomboboxcell

我想在DataGridView中的每行的两列之间创建一个依赖项。 我添加了两列(品牌,型号)运行时(从数据库中选择数据)。我的目标是在品牌细胞发生变化时在模型细胞中加载新物品。 填写数据:

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());
}

尝试清除单元格时出错。为什么会出现此错误?(添加列在运行时手动执行)。是否有其他解决方案可以解决问题?

enter image description here

1 个答案:

答案 0 :(得分:0)

我知道这是一个老问题,但仍然可以帮助下一个答案。

我刚才遇到了同样的问题,一些调试表明问题是对象的值变量。 值变量保存当前选定的组合框的值,但是当您清除项目时,该值将变为无效并导致错误。 在清除之前将value变量设置为null会删除错误。