3

我需要验证一个 gridView 列,例如该列中所有元素的总和 <=100;

如果用户输入一个值并且总和超过了我想显示自定义错误消息的限制。

我尝试在列的存储库上使用此事件编辑:

 void pinEditRepositoryItem_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        gridview1.SetColumnError(m_imixGridView.Columns["MyColumn"], "ColumnSum must be <= 100", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical);
    }

但是,当设置

              e.Cancel = true;

我收到默认消息“无效值”。

如果我使用

       gridview1.SetColumnError(m_imixGridView.Columns["MyColumn"], "ColumnSum must be <= 100", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical);

只是,错误消息是正确的,但是如果我在焦点之外单击,则会丢失。

我已经看到有多种验证行的方法,但没有找到最适合这种情况的解决方案。

如果验证失败,是否可以禁用单元格上的“不聚焦”?

非常感谢!

4

1 回答 1

7

Source

I am afraid it is difficult to determine the cause of the issue based on the provided information. Generally, the ValidatingEditor event fires when an active editor is closed, and its value is changed. The ValidateRow event fires when a current row loses focus, and some of its values has been changed.

I suggest you to go through following links:
Validating Rows
Validating Editors
BaseView.ValidatingEditor Event

Check this example:

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

private void gridView1_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e) {
    GridView view = sender as GridView;
    if(view.FocusedColumn.FieldName == "Discount") {
        //Get the currently edited value 
        double discount = Convert.ToDouble(e.Value);
        //Specify validation criteria 
        if(discount < 0) {
            e.Valid = false;
            e.ErrorText = "Enter a positive value";
        }
        if(discount > 0.2) {
            e.Valid = false;
            e.ErrorText = "Reduce the amount (20% is maximum)";
        }
    }
}

private void gridView1_InvalidValueException(object sender, InvalidValueExceptionEventArgs e) {
    //Do not perform any default action 
    e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
    //Show the message with the error text specified 
    MessageBox.Show(e.ErrorText);
}

Editor validation takes place when attempting to save the edit value (using the PostEditor method) or when closing the active editor. First, the automatic validation is performed. The editor determines whether it can accept the entered value. After the automatic validation has been performed, the ValidatingEditor event is raised. Handle this event to implement custom constraints on cell values.

If your condition fails then set the e.Valid = false; and the e.ErrorText = "ColumnSum must be <= 100"; and also handle the InvalidValueException event of gridview.

于 2012-06-20T09:47:32.353 回答