VB.net DataGridView error handling

Murray Brown
Murray Brown used Ask the Experts™

I get the following error when trying to populate a DatagridView using VB.net code.
What is the best way to handle this error?


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Team Lead
What is the best way to handle this error?
the error occurred because the value cannot be found in your DataGridViewComboBox.

to can handle it like this in DataGridView's DataError event:

Private Sub DataGridView1_DataError(sender As System.Object, e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError
        If e.Exception.Message = "DataGridViewComboBoxCell value is not valid." Then
            Dim dg As DataGridView = DirectCast(sender, DataGridView)
            Dim v As String = dg.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
            Dim c As DataGridViewComboBoxColumn = dg.Columns(e.ColumnIndex)
            If c.Items.Contains(v) = False Then
                e.ThrowException = False
            End If
        End If
    End Sub

Open in new window

Murray BrownASP.net/VSTO Developer


thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial