コード例 #1
0
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var            strValidation = string.Empty;
            string         sql           = "select context from [tb_validationrule] where name='userinfo'";
            SqlDataAdapter da            = new SqlDataAdapter(sql, strCon);
            DataTable      dt            = new System.Data.DataTable();

            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                strValidation = dt.Rows[0][0].ToString();
                //创建实例
                var expressionEditor = new UnboundColumnExpressionEditorFormEx(
                    PropertyColumnInfo.FromProperties(typeof(UserInfoEntity)), null);
                //验证的表达式
                expressionEditor.Expression = "Iif([userid] = 1 And IsNullOrEmpty([username]), '用户id为1的用户,用户名不能为空', '')";
                //在验证的页面上点击了OK之后的回调
                if (expressionEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //更新表达式到数据库
                    string        updatesql = string.Format("update [tb_validationrule] set context='{0}' where name='userinfo'", expressionEditor.Expression.Replace("'", "''"));
                    SqlConnection conn      = new SqlConnection(strCon);
                    conn.Open();
                    SqlCommand command = new SqlCommand(updatesql, conn);
                    command.ExecuteNonQuery();
                }
            }
        }
        public static PropertyColumnInfo FromProperties(Type type)
        {
            PropertyColumnInfo           eci        = new PropertyColumnInfo(typeof(object), string.Empty, true);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(type);

            foreach (PropertyDescriptor property in properties)
            {
                eci.Columns.Add(new PropertyColumnInfo(property.PropertyType, property.Name));
            }
            return(eci);
        }