C# GridView Column 验证,显示错误信息不散焦
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11114191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GridView Column validation, display error message don't loose focus
提问by Dan Dinu
I need to validate a gridView column such as the sum of all elements in the column are <=100;
我需要验证一个 gridView 列,例如该列中所有元素的总和 <=100;
If the user enters a value and the sum exceeds the limit i want to display a custom error message.
如果用户输入一个值并且总和超过限制,我想显示自定义错误消息。
I've tried using this event on the column's repositoryedit:
我已经尝试在列的存储库上使用此事件:
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);
}
However, when setting
但是,当设置
e.Cancel = true;
I get the default message "Invalid Value".
我收到默认消息“无效值”。
If i use
如果我使用
gridview1.SetColumnError(m_imixGridView.Columns["MyColumn"], "ColumnSum must be <= 100", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical);
only, the error message is correct but if i click outside the focus is lost.
只是,错误信息是正确的,但如果我在外面点击,焦点就会丢失。
I've seen that there are multiple methods for validating rows but didn't find a solution that will suit best in this case.
我已经看到有多种方法可以验证行,但没有找到最适合这种情况的解决方案。
Is it possible to disable 'unfocus' on a cell if the validation fails?
如果验证失败,是否可以在单元格上禁用“不聚焦”?
Thanks a lot!
非常感谢!
采纳答案by Niranjan Singh
I am afraid it is difficult to determine the cause of the issue based on the provided information. Generally, the
ValidatingEditorevent fires when an active editor is closed, and its value is changed. TheValidateRowevent fires when acurrent row loses focus, and some of its values has been changed.
根据提供的信息,恐怕很难确定问题的原因。通常,
ValidatingEditor当活动编辑器关闭时会触发该事件,并且其值已更改。ValidateRow当 acurrent row loses focus及其某些值已更改时,将触发该事件。
I suggest you to go through following links:
Validating Rows
Validating Editors
BaseView.ValidatingEditor Event
我建议你通过以下链接:
验证行
验证编辑器
BaseView.ValidatingEditor 事件
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 validationtakes place when attempting to save the edit value (using the PostEditormethod) 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 ValidatingEditorevent is raised. Handle this event to implement custom constraints on cell values.
尝试保存编辑值(使用PostEditor方法)或关闭活动编辑器时,会发生编辑器验证。首先,执行自动验证。编辑器确定它是否可以接受输入的值。执行自动验证后,将ValidatingEditor引发该事件。处理此事件以实现对单元格值的自定义约束。
If your condition fails then set the e.Valid = false;and the e.ErrorText = "ColumnSum must be <= 100";and also handle the InvalidValueExceptionevent of gridview.
如果您的条件失败,则设置e.Valid = false;和e.ErrorText = "ColumnSum must be <= 100";并处理InvalidValueExceptiongridview的事件。

