How to manually validate users’ input in Devexpress Xtragrids

Ingredients:
  • Developer Express 2010 or above
  • An Xtragrid
  • VS 2008
Devexpress Xtragrids can be used for users’ input. In addition to using masks to validate the inputting values entered by users, you can manually check these values by using the following events:
  • Validate single cells - gridview's ValidatingEditor event. Every time the user enter a value into a grid's cell and press tab or left-click to move to another cell in the same row this event will be raised. You will then check what column is focused and validate the user's input value.
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
private void your_grid_view_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e) {
#region - Validate one cell
if (your_gridview.FocusedColumn == columnName){
// Perform your validation here
// if the value entered by the user contains error then set
// e.Valid = false;
// e.ErrorText = "String describing error.";
// else if the value is correct then set
// e.Valid = true;
}
#endregion
// similar validating codes for other cells in the same row
}
// Catch and handle the error - when e.Valid is set to false then this event will be raised
// Users will see the error icon in the cell.
private void your_gridview_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {
e.ExceptionMode = ExceptionMode.DisplayError;
}
  • Validate a row - gridview's ValidateRow event. Every time the user enter a value into the last cell and press tab to finish adding a row or left-click to move to another cell in another row this event will be raised. You will then validate all the values entered into the cells of the grid's row.
private void gvQuanLySanPham_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) {
// perform your validation here
// if the value entered by the user contains error then set
// e.Valid = false;
// e.ErrorText = "String describing error.";
// else if the value is correct then set
// e.Valid = true;
}
// Catch and handle the error when e.Valid is set to false then this event will be raised
// Users will see the error message box "Do you want to correct the value" in the screen.
private void yourGridView_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
e.ExceptionMode = ExceptionMode.DisplayError;
}

Notes:
  • In the ValidateRow event you can use the GetRowCellValue method of the gridview to get the value entered into an individual column. Then you can validate this value.
CSharp_type valueNeedToBeValidated = (CSharp_type)yourGridView.GetRowCellValue(e.RowHandle, yourGridColumnName);
// perform validating valueNeedToBeValidated here
  • In the InvalidValueException, you can remove the error dialog box "Do you want to correct the value" by setting
    private void yourGridView_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) {
    e.ExceptionMode = ExceptionMode.NoAction;
    }
  • Summary the flow of validating cells in an Xtragrid: The user enters value into the cell and press tab -> ValidatingEditor event is raised -> in this event, if e.valid is set to false -> InvalidValueExceptions is raised -> the user sees the error icon in the cell.
  • Summary the flow of validating a row in an Xtragrid: The user enters value into the last cell and press tab -> ValidateRow event is raised -> in this event, if e.valid is set to false -> InvalidRowExceptions is raised -> the user sees the error message dialog in the screen when Exceptionmode = Exceptionmode.DisplayError.

No comments:

Post a Comment

Blog Archive

About Me

My photo
I am a software developer with roughly 5 years of experience in developing end-to-end solutions in C#, Java, C/C++, PHP and HTML/CSS/Javascript. At the moment, I am joining the Professional Doctorate in Engineering degree program in Software Technology at Eindhoven University of Technology. My areas of particular interest include software design, data structures and algorithms, problem solving, software security, embedded system, machine learning, and data science.