Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all,

i need to validate a particular column in a grid.

for eg. i hav a column named mark. Only numbers should be allowed in that column.

how to do this??????
Posted

IMHO, the grid shouldn't be involved in validation of data. The data should be validated before it's bound to the grid.

Are you getting the data from a database? Is the column a numeric value column (int, decimal, float)?
 
Share this answer
 
VB
Private Sub DtGrid_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DtGrid.EditingControlShowing
       
       Try
           Dim TB As TextBox = DirectCast(e.Control, TextBox)
           If DtGrid.CurrentCell.ColumnIndex = Ecol_Mark.Index Then
               AddHandler TB.KeyPress, AddressOf TxtMark_KeyPress
           Else
               RemoveHandler TB.KeyPress, AddressOf TxtMark_KeyPress
               RemoveHandler TB.KeyPress, AddressOf TxtMark_KeyPress
           End If
Catch ex As Exception

End Try
End Sub

Private Sub TxtMark_KeyPress(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'add your code here if e.keychar <> an integer make e.keychar = 0 
    e.KeyChar = 0
End Sub

Try This Code

[SM]: Formatted the code.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900