DataGrid validation

Jarod

Regular
Joined
Feb 5, 2003
Messages
82
Location
Bruxelles
I have a DataGrid that I would like to validate.
So I listen to the validating event.

However, my problem is that the UI changes are not reflected to the underlying datasource if the user just quit the dataGrid

For example, ona fomr with one dataGrid and one TextBox
The user clicks on the DataGrid, change one value and then click directly in the TextBox

So when I iterate thru my datasource rows, I still have the old value. I think it is because it assume the edition has not been accepted.
I tried to call AcceptChange on my row, but doesn't change anything.
So how can I do ?
Thanks,
 
Ok, here is a solution
As my datasource is a DataTable, the best to do is to validate teh DataTable itself, using the DataTable.ColumnChanging event
So:
Visual Basic:
    'dt is my DataTable, containing the data to display
    AddHandler dt.ColumnChanging, AddressOf Me.ColChange

    Me.dataGrid1.DataSource = dt
and the validation is done here:
Visual Basic:
  Private Sub ColChange(ByVal sender As Object, ByVal e As DataColumnChangeEventArgs)
    If e.Column.ColumnName = "Column to Validate" Then
      If e.ProposedValue.ToString = String.Empty Then
        e.Row.SetColumnError(e.Column, "ERROR")
      End If
    End If
  End Sub
 
Ok that's working fine but I still have a question :
this validation is done as soon as the user leaves one of the cell (to go to another cell or to another control)
But if I modify my DataGrid to make an error and focus on an another control here what's happening :
- Validation (due to ColumnChanging) --> set my column Error
- Validation (due to Row Changing) --> set my row Error
- Validation (due to DataGrid.Validating) --> if I do something like dt.HasErrors, I have the surprising result "false"

What's happening ? how to get true in that case ? (after leaving directly the first modified cell to go to another control ?)
Thanks,
 
Ok, my mistake in the previous post.
I thought that the validating event on the DataGrid was happening after ColumnChanging and Row Changing, but in fact, it occurs first !
 
Back
Top