dataGridView default error dialog handle

牧云@^-^@ 提交于 2021-01-24 06:54:49

问题


I am trying to hide default datagridview error dialog. I put in the code this event handler:

        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
    }

But still when i try this and leave datagridview cell empty ( delete everything from it), it show me dialog box with error.

Screenshot of error:


回答1:


Try to Handle and Cancel the event:

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

Also, subscribe to the event in InitializeComponent()

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



回答2:


try to use this code to handle the event:

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



回答3:


Try the next code , it's work!

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


来源:https://stackoverflow.com/questions/37776854/datagridview-default-error-dialog-handle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!