Question

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,

Was it helpful?

Solution 2

In you handler for the menuItem_click do someething like this:

private menuItem_Click(object sender, EventArgs e)
{
  gridView1.UpdateCurrentRow(); //return a bool, false = if validation error(s) was found
}

This forces the view to validate input and push it down to the datasource.

OTHER TIPS

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

The accepted answer, UpdateCurrentRow() does exactly what it says - it forces the view to update its result, returning false if it cannot due to a validation error. But this isn't the full story.

To cause a validation error, you need to use ValidateRow or ValidatingEditor. So these are used together.

The difference is ValidatingEditor works when moving between fields.

This example taken from here https://docs.devexpress.com/WindowsForms/3055/controls-and-libraries/data-grid/examples/data-editing/how-to-validate-data-entered-by-end-users

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;

private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {
    ColumnView view = sender as ColumnView;
    GridColumn column = (e as EditFormValidateEditorEventArgs)?.Column ?? view.FocusedColumn;
    if (column.Name != "colBudget") return;
    if ((Convert.ToInt32(e.Value) < 0) || (Convert.ToInt32(e.Value) > 1000000))
        e.Valid = false;
}

private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {
    ColumnView view = sender as ColumnView;
    if (view == null) return;
    e.ExceptionMode = ExceptionMode.DisplayError;
    e.WindowCaption = "Input Error";
    e.ErrorText = "The value should be greater than 0 and less than 1,000,000";
    // Destroy the editor and discard the changes made within the edited cell.
    view.HideEditor();
}

My code usually looks something like this (VB):

  Private Function ValidateView(view As ColumnView) As Boolean
        If view.IsEditing Then
            view.CloseEditor()
            Return view.UpdateCurrentRow()
        End If
        Return True
  End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top