VB.net DataGridView error handling

Hi

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

Thanks

1
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
 
Ryan ChongCommented:
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
                'c.Items.Add(v)
                e.ThrowException = False
            End If
        End If
    End Sub

Open in new window

1
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.