Datagridview组合框不更新

时间:2015-10-23 03:02:04

标签: c# .net winforms datagridview combobox

我正在使用datagridview组合框来解决问题和子问题。在选择问题时,根据问题填充其子问题组合框数据源。 enter image description here

更新子问题我正在使用

private void dataGridView2_CellValueChanged_1(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        if (e.ColumnIndex == dataGridView2.Columns[8].Index && e.RowIndex>-1)
        {
            DataGridViewComboBoxCell cell = dataGridView2.Rows[e.RowIndex].Cells[dataGridView2.Columns["KeyProblemDescription"].Index] as DataGridViewComboBoxCell;
            if (cell == null)
                return;
            Guid primaryProblem = new Guid(dataGridView2.Rows[e.RowIndex].Cells[dataGridView2.Columns["PrimaryKeyProblem"].Index].Value.ToString());
            cell.DataSource = dbCalling.getPrimaryKeyProblemDescription(primaryProblem);
            cell.DisplayMember = "Name";
            cell.ValueMember = "Id";
        }
    }
    catch (Exception)
    {
    }
}
 public DataTable getPrimaryKeyProblemDescription(Guid keyProblem)
        {
            try
            {
                using (SqlCommand com = new SqlCommand(@"SELECT [Id]  ,[KeyProblemDescription] as Name
  FROM [KeyProblemDescription] where [PsfKeyProblemId]=@keyProblem "))
                {
                    com.Parameters.AddWithValue("@keyProblem", keyProblem);
                    return selectDataTable(com);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }


public DataTable selectDataTable(SqlCommand com)
        {
            try
            {
                DataTable datatable = new DataTable();
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        com.Connection = con;
                        sda.SelectCommand = com;
                        con.Open();
                        sda.Fill(datatable);
                        return datatable;
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

当我第一次选择问题时它的工作正常,但是当我从数据库中获取问题和子问题时,它给出异常datagridview组合框子问题值无效。这背后的原因是子问题数据源没有根据问题填充。我怎么能摆脱这个。

修改

当我感觉数据源单元格值更改事件仅针对行索引-1触发时,为什么会这样?不是1,2,3 .....

3 个答案:

答案 0 :(得分:0)

代码看起来很好,除了你的第二个if语句似乎不完整。

if (cell == null)
{
    return;
}
else 
{
    Guid primaryProblem = new Guid(dataGridView2.Rows[e.RowIndex].Cells[dataGridView2.Columns["PrimaryKeyProblem"].Index].Value.ToString());
    cell.DataSource = dbCalling.getPrimaryKeyProblemDescription(primaryProblem);
    cell.DisplayMember = "Name";
    cell.ValueMember = "Id";
}

鉴于你的第二个if缺少一些括号,id预计你已经收到语法错误

答案 1 :(得分:0)

你得到的例外似乎是:

  

DataGridViewComboBoxCell值无效

在这种情况下,问题是您绑定到新的值列表,但所选的Id仍然不在新列表中。您可以通过设置cell.Value = null或直接将其设置为现有新ID来清除当前值。

private void dataGridView2_CellValueChanged_1(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        if (e.ColumnIndex == dataGridView2.Columns[8].Index && e.RowIndex>-1)
        {
            DataGridViewComboBoxCell cell = dataGridView2.Rows[e.RowIndex].Cells[dataGridView2.Columns["KeyProblemDescription"].Index] as DataGridViewComboBoxCell;
            if (cell == null)
                return;
            Guid primaryProblem = new Guid(dataGridView2.Rows[e.RowIndex].Cells[dataGridView2.Columns["PrimaryKeyProblem"].Index].Value.ToString());
            cell.Value = null; //added code
            cell.DisplayMember = "Name";
            cell.ValueMember = "Id";
            cell.DataSource = dbCalling.getPrimaryKeyProblemDescription(primaryProblem);
        }
    }
    catch (Exception)
    {
    }
}

答案 2 :(得分:0)

您可以在数据行单元格编辑事件触发时生成单独的组合框值。以下代码将生成父代和子代,但您可以在编辑时自动生成子代,并使用其他方法填充父代,并且不会在每次编辑时更改它。

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {

        // Parent Problem Combobox
        if (e.ColumnIndex == x && e.RowIndex != -1)
        {
            DataGridViewComboBoxCell cbx = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[indexofparent];
            if (cbx?.Items != null)
                cbx.Items.Clear();


            // Populate RESULT datatable from a SQL query

            foreach (DataRow item in result.Rows)
            {
                if (!string.IsNullOrWhiteSpace(item[0].ToString()))
                    cbx.Items.Add(item[0].ToString());
            }
        }
        // Populate child
        else if (e.ColumnIndex == x && e.RowIndex != -1)
        {
            DataGridViewComboBoxCell cbx = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[indexofchild];
            if (cbx?.Items != null)
                cbx.Items.Clear();

            // Populate RESULT datatable from a SQL query

            foreach (DataRow item in result.Rows)
            {
                if (!string.IsNullOrWhiteSpace(item[0].ToString()))
                    cbx.Items.Add(item[0].ToString());
            }


        }
    }