【问题标题】:Set ComboBox in Specific Cell in a DataGridView在 DataGridView 的特定单元格中设置 ComboBox
【发布时间】:2018-01-01 23:10:03
【问题描述】:

上周我问了同样的问题,感谢用户@Aaron,我最终得到了解决我的问题的方法。但是,我再次询问,因为代码在一个项目中完美工作,但在几乎完全相同的条件下(即列/行数、变量类型、如何DGV 已填充)。

//This is my code to go through each cell in the DataGridView.
for (int i = 0; i < dgvTest.RowCount; i++)
        {
            for (int j = 0; j < dgvTest.ColumnCount; j++)
            {
                foreach (Information info in frmMain._dbList)
                {
                    if (dgvTest.Rows[i].Cells[j].Value.ToString().ToLower() == info.InfoName.ToLower() && info.InfoInputType == "1")
                    {
                        DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
                        c.Items.Add("0");
                        c.Items.Add("1");
                        dgvTest.Rows[i].Cells[(j + 1)] = c;

                    }
                }
            }
        }

问题:

一旦我奇怪地单击“确定”,它就会创建 ComboBox。如果我重复这个过程,它最终会用 ComboBox 填充每个单元格,但是每当我将鼠标悬停在它们上面时,都会弹出相同的错误消息。

是否将单元格设置为组合框,然后尝试返回同一个单元格?

已解决

简单的解决方案 - 必须添加一个 c.Value = # 来设置值。

【问题讨论】:

    标签: c# winforms visual-studio datagridview combobox


    【解决方案1】:

    为了避免上述问题。您可以为您的DataGridView 添加DataError 事件。

    试试下面的代码:

    private void dgvTest_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        try
        {
            if (e.Exception.Message.contains("DataGridViewComboBoxCell value is not valid."))
            {
                object value = dgvTest.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (!((DataGridViewComboBoxColumn)dgvTest.Columns[e.ColumnIndex]).Items.Contains(value))
                {
                    ((DataGridViewComboBoxColumn)dgvTest.Columns[e.ColumnIndex]).Items.Add(value);
                }
            }
    
            throw e.Exception;
        }
        catch (Exception ex)
        {
            MessageBox.Show(string.Format(@"Failed to bind ComboBox. "
                + "Please contact support team with below error message:"
                + "\n\n" + ex.Message));
        }
    }
    

    以上,将向用户抛出错误消息。如果你想抑制这个错误。您只需按照以下代码:

    private void dgvTest_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        try
        {
            if (e.Exception.Message.contains( "DataGridViewComboBoxCell value is not valid."))
            {
                object value = dgvTest.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (!((DataGridViewComboBoxColumn)dgvTest.Columns[e.ColumnIndex]).Items.Contains(value))
                {
                    ((DataGridViewComboBoxColumn)dgvTest.Columns[e.ColumnIndex]).Items.Add(value);
                }
            }
        }
        catch (Exception ex)
        {
            //do nothing
        }
    }
    

    【讨论】:

    • 感谢您的快速回复!我在 ExceptionPolicy 上遇到错误?这是 Visual Studio 的内置功能还是 HandleException 是包含在类 ExceptionPolicy 中的方法?
    • @Yahtzee 不需要它。您只需处理该异常并显示消息框。我已经修改了答案。
    • 我不认为异常被击中,因为错误消息不是那么简单。我编辑了我的问题并上传了错误消息的图片。
    • 请通过添加DataError我在sn-p下面提到的事件来抑制错误消息。
    • @Balaguranathan Marimuthu 该方法没有被命中,因为 e.Exception.Message 不等于对话框中的那个。
    【解决方案2】:

    必须将 ComboBox 的初始值设置为某个值。

    c.Value = #
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      相关资源
      最近更新 更多