Search code examples
vb.netdatagridviewerror-checking

How to Supply Default Value back to DataGridViewCell After Error Message


My current code will show a messagebox that tells the user that only numeric input will be accepted if they input any non-integer into the field. However I can't seem to find out how I might supply a default value back to the datagridviewcell.

Private Sub DataGridViewCOTS_DataError(ByVal sender As Object, ByVal e As DataGridViewDataErrorEventArgs) Handles DataGridViewCOTS.DataError

    If StrComp(e.Exception.Message, "Input string was not in a correct format.") = 0 Then

        'If e.ColumnIndex = 2 Then
        MessageBox.Show("Please Enter a numeric Value")


        'DataGridViewCOTS.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 3

    End If





End Sub

Solution

  • From this thread http://social.msdn.microsoft.com/Forums/windows/en-US/ca2b3a41-e598-4a84-ac75-57064febd5e5/change-cell-value-in-datagridviewdataerror

    The DataError event occurs when an external data-parsing or validation operation throws an exception, or when an attempt to commit data to a data source fails.

    Based on it, you can't fix the value here since the value will not be committed. After you changed the correct/default value to the DataGridView Cell, you must commit it. So, set the e.Cancel = false would be OK.

    So, just set e.Cancel = false after you change value and it should work.

    Hope this helps