Windows Forms DataGrid validation
Hi,
I'm using VB.NET 2005 Express, and I want to create a price break DataGrid having three columns, with an unknown number of rows.
In column 0, I want "From"
In column 1, I want "To"
In column 2, I want "Price"
That bit is easy enough.
The complicated bit, for me, is applying a min and max to the "From" and "To" cells. So, for example, if the user has entered ...
From|To|Price
1|9|5.00
... on the first row, when the user comes to add the next row, I want the "From" cell on the new row to only accept a value greater than 9, so the table may look like ...
From|To|Price
1|9|5.00
10|20|4.50
Any help would be greatly appreciated.
# 3 Re: Windows Forms DataGrid validation
OK, assuming you're binding a Windows Forms DataGrid to an Access table named "Table", using a DataSet named "DataSet1," you need to add a method to handle the DataTable's ColumnChanging event:
' Add this line to your Form_Load event
AddHandler DataSet1.Tables("Table").ColumnChanging, _
AddressOf Table_ColumnChanging
Private Sub Table_ColumnChanging(ByVal sender As Object, _
ByVal e As System.Data.DataColumnChangeEventArgs)
Dim PreviousToValue As Integer = 0
Dim RowIndex As Integer = DataGrid1.CurrentRowIndex
' If not first row of grid...
If RowIndex > 0 Then
' Get value of second column of previous row
PreviousToValue = CInt(DataSet1.Tables("Table").Rows(RowIndex - 1).Item(1))
End If
If (e.Column.ColumnName.Equals("From")) Then
If CType(e.ProposedValue, Integer) <= PreviousToValue Then
e.Row.RowError = "The From column contains an error"
e.Row.SetColumnError(e.Column, "From value must be greater than To value of previous row.")
Else
e.Row.ClearErrors()
End If
End If
End Sub
# 9 Re: Windows Forms DataGrid validation
OK, this code seems to work for the first row, but throws an IndexOutOfRangeException for subsequent rows:
Private Const FROM_COLUMN As Integer = 0
Private Const TO_COLUMN As Integer = 1
Private Const PRICE_COLUMN As Integer = 2
Private Sub Table_ColumnChanging(ByVal sender As Object, _
ByVal e As System.Data.DataColumnChangeEventArgs)
Dim PreviousToValue As Integer = 0
Dim RowIndex As Integer = DataGrid1.CurrentRowIndex
' If not first row of grid...
If RowIndex > 0 Then
' Get value of second column of previous row
PreviousToValue = CInt(DataSet1.Tables("Table").Rows(RowIndex - 1).Item(TO_COLUMN))
End If
Select Case e.Column.Ordinal
Case FROM_COLUMN
If CType(e.ProposedValue, Integer) <= PreviousToValue Then
e.Row.RowError = "The From column contains an error"
e.Row.SetColumnError(e.Column, "From value must be greater than To value of previous row.")
Else
e.Row.RowError = ""
e.Row.SetColumnError(e.Column, "")
End If
Case TO_COLUMN
Dim FromValue As Integer = CInt(DataSet1.Tables("Table").Rows(RowIndex).Item(FROM_COLUMN))
If CType(e.ProposedValue, Integer) <= FromValue Then
e.Row.RowError = "The To column contains an error"
e.Row.SetColumnError(e.Column, "To value must be greater than From value.")
Else
e.Row.RowError = ""
e.Row.SetColumnError(e.Column, "")
End If
End Select
End Sub
Try this:
Private Sub Table_ColumnChanging(ByVal sender As Object, _
ByVal e As System.Data.DataColumnChangeEventArgs)
' Remember From column value between method calls
Static FromValue As Integer
Dim PreviousToValue As Integer = 0
Dim RowIndex As Integer = DataGrid1.CurrentRowIndex
' If not first row of grid...
If RowIndex > 0 Then
' Get value of second column of previous row
PreviousToValue = CInt(DataSet1.Tables("Table").Rows(RowIndex - 1).Item(TO_COLUMN))
End If
Select Case e.Column.Ordinal
Case FROM_COLUMN
If CType(e.ProposedValue, Integer) <= PreviousToValue Then
e.Row.RowError = "The From column contains an error"
e.Row.SetColumnError(e.Column, "From value must be greater than To value of previous row.")
Else
e.Row.RowError = ""
e.Row.SetColumnError(e.Column, "")
End If
' Save From value for To column validation below
FromValue = CType(e.ProposedValue, Integer)
Case TO_COLUMN
If CType(e.ProposedValue, Integer) <= FromValue Then
e.Row.RowError = "The To column contains an error"
e.Row.SetColumnError(e.Column, "To value must be greater than From value.")
Else
e.Row.RowError = ""
e.Row.SetColumnError(e.Column, "")
End If
End Select
End Sub