DevExpress实现GridControl单元格编辑验证的方法

时间:2021-05-20

本文实例演示了DevExpress实现GridControl单元格编辑验证的方法,比较实用的功能,具体方法如下:

主要功能代码如下:

/// <summary>/// 自定义单元格验证/// </summary>/// <param name="view">GridView</param>/// <param name="e">BaseContainerValidateEditorEventArgs</param>/// <param name="fieldNameHandler">委托</param>/// <param name="errorHanlder">委托</param>/// <param name="errorText">当验证不通过对时候,错误提示信息文字</param>public static void CustomValidatingEditor(this GridView view, BaseContainerValidateEditorEventArgs e, Predicate<string> fieldNameHandler, Predicate<object> errorHanlder, string errorText){ /*说明 *在ValidatingEditor事件使用 *eg: *string[] workType = new string[4] { "-1", "关闭但不删除", "启用", "删除" }; *void gvLampConfig_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e) *{ * GridView _view = sender as GridView; * _view.CustomValidatingEditor(e, fieldName => fieldName.Equals("TLampWorkStatus"), value => !workType.Contains<string>(value.ToString()), "若想设置为不修改,请输入-1即可"); * } */ if (fieldNameHandler(view.FocusedColumn.FieldName)) { if (errorHanlder(e.Value)) { e.Valid = false; e.ErrorText = errorText; } }}

代码使用方法如下:

string[] workType = new string[4] { "-1", "关闭但不删除", "启用", "删除" };void gvLampConfig_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e){ GridView _view = sender as GridView; _view.CustomValidatingEditor(e, fieldName => fieldName.Equals("TLampWorkStatus"), value => !workType.Contains<string>(value.ToString()), "若想设置为不修改,请输入-1即可");}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章