Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Datagridview组合框未更新_C#_.net_Winforms_Datagridview_Combobox - Fatal编程技术网

C# Datagridview组合框未更新

C# Datagridview组合框未更新,c#,.net,winforms,datagridview,combobox,C#,.net,Winforms,Datagridview,Combobox,我使用datagridview组合框处理问题和子问题。在选择问题时,根据问题填充子问题组合框数据源 用于更新我正在使用的子问题 private void dataGridView2_CellValueChanged_1(object sender, DataGridViewCellEventArgs e) { try { if (e.ColumnIndex == dataGridView2.Columns[8].Index && e.RowInde

我使用datagridview组合框处理问题和子问题。在选择问题时,根据问题填充子问题组合框数据源

用于更新我正在使用的子问题

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;
            }
        }
当我第一次选择problem时,它工作正常,但当我从数据库中获取problem和sub problem时,它给出的异常datagridview combobox sub problem值无效。这背后的原因是子问题数据源并没有根据问题进行填充。我怎样才能摆脱这个

编辑


当我感觉datasource单元格值更改事件只针对行索引-1触发时,为什么会这样呢??不适用于1,2,3….

代码看起来基本正常,只是第二个if语句似乎不完整

试一试


如果您的第二个
缺少一些括号,我希望您已经出现语法错误

DataGridViewComboxCell值无效

在这种情况下,问题是您绑定到一个新的值列表,但所选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)
    {
    }
}

当datarow单元格编辑事件触发时,您可以只生成单个组合框值。下面的代码将同时生成父级和子级,但您可以在编辑时自动生成子级,并使用不同的方法填充父级,而不是每次编辑时都对其进行更改

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


        }
    }

如果只使用一条语句,则不必使用括号。也不必使用
else
,因为如果
单元格
,则他使用
返回
。所以代码无论如何都不会继续。啊哈,我明白了。我被教导要总是封装——我猜这不会有什么伤害,尽管我知道在这种情况下不需要其他东西getPrimaryKeyProblemDescription的类型是什么?绑定列表?能否显示getPrimaryKeyProblemDescription代码?请检查我的编辑..尝试添加手动刷新:cell.DisplayMember=“Name”;cell.ValueMember=“Id”;cell.Items.Refresh();我应该在何时进行手动刷新。在cell.ValueMember=“Id”之后;调用cell.Items.Refresh();
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());
            }


        }
    }