How can i limit the user to enter only certain data in a datagrid. Lets say 0 - 9 only. When the user try's to enter any other char, it should stay 0
Printable View
How can i limit the user to enter only certain data in a datagrid. Lets say 0 - 9 only. When the user try's to enter any other char, it should stay 0
Catch DataGridView_CellValidating event and check the value.
Example here:
http://msdn.microsoft.com/en-us/library/7ehy30d4.aspx
THis is the code i got so far:
BUT!!.. how do i fill the empty cell with a value of 0. As you can see the line i used does not work!!Code:Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If e.FormattedValue IsNot Nothing AndAlso String.IsNullOrEmpty(e.FormattedValue.ToString()) Then
DataGridView1.Rows(e.RowIndex).ErrorText = "Absent Reason must not be empty"
e.Cancel = True
'DataGridView1.CurrentCell.value = 0 ----> THIS DOES NOT WORK !!!
End If
End Sub
I wish people stopped using 'does not work' phrase and supplied real error mesages instead.
P.S.
You don't need to change the value, by the way. The validation procedure won't allow user to leave this cell until he supplies the correct value.
And I wish 'PEOPLE' would understand that when 'It does not Work', it means :mad:
....IT DOES NOT DO WHAT IT WANT!!
That does not mean that a Error is generated, or even that the program stops...!!!
I want it to replace an empty space or any other character that is NOT 0-9 with a 0.
Does that help:
Not particularly.
I tried it and it worked for me.
Just out of curiosity - in your code above this line is commented. Are you sure you got it uncommented?
Try this:
Code:DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex ).Value = 0