Reputation: 652
I have a datagridview bound to a dataset. My goal for my datagridview is to prevent the user from entering negative integers and leaving the datagridviewcell blank. I wish to have some sort of error message or message-box to tell the user that their entry is invalid. Here is what I have so far, hopefully it can give you a starting point. I greatly appreciate any help or suggestions you may give.
Private Sub DataGridView1_DataError(ByVal sender As Object, _
ByVal e As DataGridViewDataErrorEventArgs) _
Handles DataGridView1.DataError
If CInt(e.Exception.Message, "Input string was not in a correct format.") < 0 Then
MessageBox.Show("Please Enter a positive Value")
'This will change the number back to original
DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = " "
End If
Upvotes: 0
Views: 3249
Reputation: 14088
Have a look at the documentation for DataGridView's CellValidating
event: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx
Attaching a handler for this event allows you to check whether the new value is:
(in that order) and cancel the change if any one of those conditions are true.
EDIT
I'm not sure if this is a coincidence, but you can just copy and paste the example code from the documentation. It does exactly what you're trying to do.
Upvotes: 1