Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vb2008 datagridView cell validating

Status
Not open for further replies.

nicholasting

Programmer
Jul 28, 2006
15
0
0
MY
i'm using datagrid view to display some records that query from database.

[Product Name], [Qty On Hand]
the column [Qty On Hand] is a numeric column. in the grid i allow user to imediate change any numeric value as they wish. but if use key in character, the system return error. i was trying to use CellValidating but still unable to validate the data key in. what is the event to use to check whether the data key in is a numeric?

i coded as below, but not work:

Private Sub dgdProduct_CellValidating(...)...
If Not IsNumeric(dgdProduct.CurrentRow.Cells("QTY").Value) Then
dgdProduct.CurrentRow.Cells("QTY").Value = 0
End If
End Sub

the propertise CurrentRow.Cells("QTY") is still holding the data before i edited.

if i use dgdCSBarcode_CellLeave, the system already return system error message before go into this event.

thanks for comments.

regards,
nic
 
First thing to note is that while working with UNSAVED or DIRTY) data in a DataGridView, you want to look at the .EditedFormattedValue property instead of just .Value. This property holds the current value as edited based on the rules of the textbox. Someone here in the forums turned me to this property and I thank him/her every day. [smile]

I do something similar in a application of mine, and I use the CellLeave event to do the check the value. Here's my code from the CellLeave event:

Code:
        If Me.dgvItems.CurrentRow.Cells(0).EditedFormattedValue.ToString.Length > 0 AndAlso Not IsNumeric(Me.dgvItems.CurrentRow.Cells(0).EditedFormattedValue) Then
            Me.dgvItems.CurrentRow.Cells(0).Value = 0
        End If

This may not be the best method, but it seems to work for me.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top