DoLoop
DoLoop

Reputation: 125

dataGridView default error dialog handle

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:

enter image description here

Upvotes: 4

Views: 18701

Answers (4)

TRexF14
TRexF14

Reputation: 47

For my application, it was simplest for me to simply apply the "?" nullable-value-type modifier to the properties of the class being used in my Data Grid, and this resolved the issue. This way, the user could enter an "empty" value for an integer - without the grid griping about it. (I have my own custom fluent validation for all of my classes).

public class Machine
{
    public string? Name { get; set; }
    public string? IPAddress { get; set; }
    public int? Port { get; set; }
}

Upvotes: 0

Shaharyar
Shaharyar

Reputation: 12449

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

Upvotes: 12

kammer
kammer

Reputation: 11

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

Upvotes: 1

Mohsen Karimi
Mohsen Karimi

Reputation: 11

try to use this code to handle the event:

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

Upvotes: 1

Related Questions