【问题标题】:dataGridView default error dialog handledataGridView 默认错误对话框句柄
【发布时间】:2016-06-12 16:58:48
【问题描述】:

我正在尝试隐藏默认的 datagridview 错误对话框。 我把这个事件处理程序的代码:

        this.dataGridView2.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(dataGridView2_DataError);


    private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        //empty so it doesn't show anything
    }

但是当我尝试这个并将 datagridview 单元格留空(从中删除所有内容)时,它仍然显示错误对话框。

错误截图:

【问题讨论】:

  • 我们可以假设您的名字是正确的并且确实将一个事件与两个 DGV 挂钩吗?
  • @TaW 哦,我错过了,我的脑袋为什么不隐藏对话框..
  • 哦,我现在明白了。我在 dgv1 上钩住了事件,并在 dgv2 上对其进行了测试。谢谢。

标签: c# datagridview


【解决方案1】:

尝试处理并Cancel 事件:

private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    e.Cancel = true;
}

另外,订阅InitializeComponent()中的事件

private void InitializeComponent()
{
   //...
   this.dataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView2_DataError);
}

【讨论】:

  • 还是一样。 DataGridView 默认对话框仍然显示。
  • 可以附上截图吗?
  • @DoLoop 你有多少个 GridView?您是否将所有这些人都订阅了此活动?
  • 在那种形式 2 中,我正在使用 1 和 1 用于测试(datagridview2)
  • 您必须同时订阅此活动。你有吗?
【解决方案2】:

尝试使用此代码来处理事件:

private void dataGridView2_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    e.Cancel = true;
}

【讨论】:

    【解决方案3】:

    试试下一个代码,成功了!

     private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            try
            {
                //To handle 'ConstraintException' default error dialog (for example, unique value)
                if ((e.Exception) is System.Data.ConstraintException)
                {
                    // ErrorText glyphs show
                    dataGridView1.Rows[e.RowIndex].ErrorText = "must be unique value";
                    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ErrorText = "must be unique value";
    
                    //...or MessageBox show
                    MessageBox.Show(e.Exception.Message, "Error ConstraintException",
                                                   MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //Suppress a ConstraintException
                    e.ThrowException = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR: dataGridView1_DataError",
                                         MessageBoxButtons.OK, MessageBoxIcon.Error);
            }         
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多