This is a migrated thread and some comments may be shown as answers.

Validation with business rules in dataset

2 Answers 136 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ubaldo
Top achievements
Rank 1
Ubaldo asked on 21 Jan 2011, 10:47 AM

I've got a simple RadGridView bound to a DataTable (through a BindingSource); standard and easy way to put a gridview on a form dragging it from datasources box.
I've implemented my business rule validation in the datatable, as below:

Private Sub OperazioniDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging
  Debug.Print("ColumnChanging: column=" & e.Column.ColumnName)
  If (e.Column.ColumnName = Me.impovalColumn.ColumnName) Then
    If CType(e.ProposedValue, Decimal) = 0 Then
      e.Row.SetColumnError(e.Column, "Inserire un importo diverso da zero")
    Else
      e.Row.SetColumnError(e.Column, "")
    End If
  End If
End Sub

When I edit the [impoval] cell and enter 0, moving to another cell/row raises the ColumnChanging event, but no feedback at all on the grid.
Another issue: if I press [Escape] key in the cell, the value is not restored to the old one.
In the standard MS grid I get feedback and restore behaviour.
I would like to know the best practices to enforce data validation (on single cells and on row, moving away from a cell/row and/or trying to close a form, etc.), possibly avoiding to put rules in the gridview events, in order to keep all the business rules validation code in the data transfer objects.
Please don't suggest to create separate business objects, as I want to avoid this coding overhead.
TIA
Ubaldo

2 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 26 Jan 2011, 01:48 PM
Hello Ubaldo,

You should validate the data through the grid or your data source should implement the IDataErrorInfo interface. You should use the the CellValidating event to achieve the desired scenario:

Private Sub radGridView1_CellValidating(ByVal sender As Object, ByVal e As CellValidatingEventArgs)
    If e.Column.Name = "ID" Then
        Dim value As Decimal = Convert.ToDecimal(e.Value)
 
        If value = 0 Then
            e.Cancel = True
            e.Row.ErrorText = "Inserire un importo diverso da zero"
            e.Row.Cells("ID").Style.CustomizeBorder = True
            e.Row.Cells("ID").Style.DrawBorder = True
            e.Row.Cells("ID").Style.BorderGradientStyle = Telerik.WinControls.GradientStyles.Solid
            e.Row.Cells("ID").Style.BorderWidth = 3
            e.Row.Cells("ID").Style.BorderColor = Color.Red
        Else
            e.Row.ErrorText = Nothing
            e.Row.Cells("ID").Style.Reset()
        End If
    End If
End Sub

I hope this helps.

Greetings,
Svett
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
0
Richard Slade
Top achievements
Rank 2
answered on 26 Jan 2011, 01:58 PM
Hello,

In addition to Svett's reply, there is a Code Library Project here on using RadGridView and IDataErrorInfo
Regards,
Richard
Tags
GridView
Asked by
Ubaldo
Top achievements
Rank 1
Answers by
Svett
Telerik team
Richard Slade
Top achievements
Rank 2
Share this question
or