Search code examples
c#datagridview

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


Solution

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