|
-
Sep 24th, 2008, 11:03 AM
#1
Thread Starter
Hyperactive Member
[2008] Validating integer and decimal fields in datagridview cell
I'm wondering what is the best practice for validating a number field in a datagridview cell. I'm using the CellValidating event and testing for the columns that are number fields. I'll just show my code.
Code:
Private Sub grdPOLineItems_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles grdPOLineItems.CellValidating
Try
If Me.grdPOLineItems.Rows.Count < 1 Then Exit Sub
'is the user clicking on the column headers?
If e.RowIndex = -1 Then Exit Sub
'is it the new row?
If Me.grdPOLineItems.Rows(e.RowIndex).IsNewRow Then Exit Sub
If e.FormattedValue = Nothing Then Exit Sub
'here is where we divide the waters between decimal price fields and integer quantity fields
If e.ColumnIndex = Me.grdPOLineItems.Columns("clmUnitPrice").DisplayIndex Then
Dim newValue As Decimal
If Decimal.TryParse(e.FormattedValue, newValue) Then
'are there no changes being made?
If newValue = CDec(Me.grdPOLineItems.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Then Exit Sub
Else
'if the value can't be parsed into a decimal, then it is invalid
e.Cancel = True
End If
ElseIf Me.integerColumns.Contains(e.ColumnIndex) Then
'deals with integer columns
Dim newValue As Integer
If Integer.TryParse(e.FormattedValue, newValue) Then
If newValue = CInt(Me.grdPOLineItems.Rows(e.RowIndex).Cells(e.ColumnIndex).Value) Then Exit Sub
Else
e.Cancel = True
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, AppName)
End Try
End Sub
The lines in green are simple tests I use to determine if there is any point in continuing with the procedure.
The "clmUnitPrice" is a decimal field column. The integerColumns collection is merely a list of integer values, initialized earlier on, that holds the display indices of a list of columns that only allow integer values.
The problem I am having is with the line in red. Say, for instance, a user enters a cell that is tied to an Int32 or Decimal typed column. In this cell the user types a value, then decides, no, this is the wrong column, so the user backspaces to String.Empty and tries to tab to the next column. I'm getting a massive cell error stating that "" cannot be converted to type Int32. What boggles me is, if the user enters that cell, and then decides to tab out with only string.empty as the value, the system accepts it just fine. But if they type and then backspace to string.empty, then it snags.
I replaced the red code with the following:
If e.formattedvalue = nothing then
e.cancel = true
exit sub
end if
This code makes the system really snag. Then it won't even let me exit my application by clicking the "X" in the control box. I have to close it through Visual Studio.
Anyway, my question is, first, maybe, is there anything obviously wrong with my code? Or second, is there a more standard and better way to validate Cell Values in VB?
Thanks,
Jerome
Last edited by Psychlotron; Sep 24th, 2008 at 11:10 AM.
-
Sep 24th, 2008, 11:10 AM
#2
Thread Starter
Hyperactive Member
Re: [2008] Validating integer and decimal fields in datagridview cell
Also: I know I could just teach the users to press the escape key when they're in a cell to cancel the edit, but that merely resets the value to what it was before they were editing. What if they really want to clear an existing value out? The code that I have will not allow them to ever clear a value out. They would have to enter 0 as the new value, which seems contrived and unprofessional, etc.
Any ideas?
Thanks,
Jerome
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|