Assume that a Grid View contains two columns: "Units In Stock" and "Units On Order". A value in the first column must be more than the value of the second one. So we need to perform validation of a row when it is about to be saved to the data source. For this purpose, the ColumnView.ValidateRow event is handled.
If row fails validation, we set errors for the columns with coresponding descriptions using the ColumnView.SetColumnError method. The descriptions will be displayed when hovering over error icons.
The ColumnView.InvalidRowException event is handled in order to suppress displaying the default error message box.
The following screenshot shows a Grid View after a row fails validation.
If row fails validation, we set errors for the columns with coresponding descriptions using the ColumnView.SetColumnError method. The descriptions will be displayed when hovering over error icons.
The ColumnView.InvalidRowException event is handled in order to suppress displaying the default error message box.
The following screenshot shows a Grid View after a row fails validation.
C#
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Controls;
private void gridView1_ValidateRow(object sender,
DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
GridView view = sender as GridView;
GridColumn inStockCol = view.Columns["UnitsInStock"];
GridColumn onOrderCol = view.Columns["UnitsOnOrder"];
//Get the value of the first column
Int16 inSt = (Int16)view.GetRowCellValue(e.RowHandle, inStockCol);
//Get the value of the second column
Int16 onOrd = (Int16)view.GetRowCellValue(e.RowHandle, onOrderCol);
//Validity criterion
if (inSt < onOrd) {
e.Valid = false;
//Set errors with specific descriptions for the columns
view.SetColumnError(inStockCol, "The value must be greater than Units On Order");
view.SetColumnError(onOrderCol, "The value must be less than Units In Stock");
}
}
private void gridView1_InvalidRowException(object sender,
DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
//Suppress displaying the error message box
e.ExceptionMode = ExceptionMode.NoAction;
}
VB
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors.Controls
Private Sub GridView1_ValidateRow(ByVal sender As Object, _
ByVal e As DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs) _
Handles GridView1.ValidateRow
Dim view As GridView = CType(sender, GridView)
Dim inStockCol As GridColumn = View.Columns("UnitsInStock")
Dim onOrderCol As GridColumn = View.Columns("UnitsOnOrder")
'Get the value of the first column
Dim inSt As Int16 = CType(view.GetRowCellValue(e.RowHandle, UnitsInStock), Int16)
'Get the value of the second column
Dim onOrd As Int16 = CType(view.GetRowCellValue(e.RowHandle, UnitsOnOrder), Int16)
'Validity criterion
If inSt < onOrd Then
e.Valid = False
'Set errors with specific descriptions for the columns
View.SetColumnError(inStockCol, "The value must be greater than Units On Order")
View.SetColumnError(onOrderCol, "The value must be less than Units In Stock")
End If
End Sub
Private Sub GridView1_InvalidRowException(ByVal sender As Object, _
ByVal e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) _
Handles GridView1.InvalidRowException
'Suppress displaying the error message box
e.ExceptionMode = ExceptionMode.NoAction
End Sub
No comments:
Post a Comment