首页 > 代码库 > [DevExpress]GridControl单元格编辑验证

[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 =http://www.mamicode.com/> !workType.Contains(value.ToString()), "若想设置为不修改,请输入-1即可");             * }             */            if (fieldNameHandler(view.FocusedColumn.FieldName))            {                if (errorHanlder(e.Value))                {                    e.Valid = false;                    e.ErrorText = errorText;                }            }        }

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>代码使用:

        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即可");        }
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>