Validating cells in DevExpress GridControl

danijepg picture danijepg · Mar 8, 2013 · Viewed 15.3k times · Source

Just a simple question I can't manage myself.

I have a DevExpress GridControl for Winforms (12.2) filled with some numeric values, the grid is editable and the user can change those values.

Imagine the user changes one, what I want is to validate this cell in order to get the corresponding value modified in my datasource without clicking aoutside the cell.

That is to say, I want the user to be able to validate and apply all the values just pressing a button in a toolbar, not clicking enter, esc or clicking in the table.

I was looking some forums and didn't get the correct answer

Thanks,

Answer

wterbeek picture wterbeek · Mar 8, 2013

It depends on what you want to do. you have 2 options. either validate the row and return a messagebox displaying an error message. or you can have that little red 'x' inside the cell

both methods would work. but require slightly different implementations. both methods require you to subscribe to the Validate row event of the gridview, not the gridcontrol.

something like this would give you a textbox;

private void gridView1_ValidateRow(object sender,
DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) 
{
    e.Valid = false;
}

and something like this would give you the red 'x' in the cell;

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) {
        //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");
    }
}

the information was found here: http://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsBaseColumnView_ValidateRowtopic

this would still require the user to exit the cell,

i found some more information here: http://www.devexpress.com/Support/Center/p/A289.aspx