3

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,

danijepg
  • 347
  • 1
  • 4
  • 15

3 Answers3

2

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

wterbeek
  • 451
  • 9
  • 26
1

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.

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
  • For me this method dumps the current value of the datasource object to the row overriding the user value. I am looking for the reverse behaviour. UpdateCurrentRow is returning true. I have checked the DevExpress documentation and seems that you are rigth with your explanation, but is still not working, any ideas? – danijepg Mar 08 '13 at 11:16
  • @danijepg are you handeling any other events on the gridview? – Jens Kloster Mar 08 '13 at 11:46
  • Mouseclick, RowCellStyle and CustomDrawCell, but none of them have nothing to do with validating cells. The Mouseclick is only for a menu. The cell I'm trying to modify has no custom editor , is an integer – danijepg Mar 08 '13 at 11:57
  • 1
    Forgot it, the setter of this property is throwing an odd exception due to other problem. The updateCurrentRow is working fine, so your solution is rigth. thanks!!! – danijepg Mar 08 '13 at 12:02
0

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
Philip Johnson
  • 1,091
  • 10
  • 24