关于DevExpress GridView事件

 

//RowCellStyle 行单元格样式事件
//用于设置行单元格样式
//这里举例 如果状态为0则白背景色改为蓝绿色
 
if(e.RowHanle>=0)//当前视图的所有行
 {
     DataRow dr =this.view.GetRow(e.RowHanle);//获取所有行的数据
    if(dr==null) return;
     if(dr["AGE"]==0) 
     {
           e.Appearance.Backcolor=Color.YellowGreen;
     }       
 }

 

 //ValidatingEditor 验证编辑器事件   
 //验证view中某一列的值不能相同 
 //例:用户编号不能相同 USER_CODE
 private void viewUser_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
         {
             //如果当前焦点列为用户编码列USER_CODE 
             if (this.viewUser.FocusedColumn == this.viewUser.Columns[DE.GmUser.USER_CODE])
             {
                 //e.Value当前列的值
                 if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString())) return;
 
                 string usrCode = e.Value.ToString();
                 int currowHandler = this.viewUser.FocusedRowHandle;//当前焦点行的行号
                 int rowHandler = this.viewUser.LocateByValue(0, this.viewUser.FocusedColumn, usrCode);//从第一行开始查找该列相同的userCode,返回行号。view.(从哪里开始查找,列名,要查找的值)
 
                 if (rowHandler > -1)
                 {
                     if (rowHandler != currowHandler)
                     {
                         if (this.viewUser.FocusedValue != null)
                         {
                             //提示信息:用户编号不能重复
                         }
                         e.Valid = false;//提示单元格的值无效 单元格会出现一个红色的×
                         this.viewUser.HideEditor();//隐藏编辑器,放弃次单元格所做的更改
                         this.viewUser.ShowEditor();//显示编辑器
                     }
 
                 }
                 else
                 {
                     rowHandler = this.viewUser.LocateByValue(this.viewUser.RowCount - 1, this.viewUser.FocusedColumn, usrCode);//反过来查找
 
                     if (rowHandler > -1 && rowHandler != currowHandler)
                     {
                         if (this.viewUser.FocusedValue != null)
                         {
                             //提示信息:用户编号不能重复
                         }
                         e.Valid = false;
                         this.viewUser.HideEditor();
                         this.viewUser.ShowEditor();
                     }
                 }
             }
 
         }
 
验证编辑器事件(单列)
 //ValidatingEditor 验证编辑器事件   
 //验证view中某多列的值不能相同 
 
 private void viewOrgTarget_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
         {
             if (this.viewOrgTarget.FocusedColumn == this.viewOrgTarget.Columns[DE.BmOrgTarget.OPER_DATE])
             {
                 int intHandle = this.viewOrgTarget.FocusedRowHandle;
                 this.viewOrgTarget.FocusedRowHandle = -1;
                 this.viewOrgTarget.FocusedRowHandle = intHandle;
 
                 if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString())) return;
 
                 DataRow drCurrent = this.viewOrgTarget.GetFocusedDataRow();
 
                 string strBrandId = TWays.Utils.ToString(drCurrent[DE.BmOrgTarget.BRAND_ID]);
                 string strBranchId = TWays.Utils.ToString(drCurrent[DE.BmOrgTarget.BRANCH_ID]);
                 string strCateId = TWays.Utils.ToString(drCurrent[DE.BmOrgTarget.CATE_ID]);
                 string strOrgType = TWays.Utils.ToString(drCurrent[DE.BmOrgTarget.ORG_TYPE]);
                 string strOperDate = e.Value.ToString();
                 if (string.IsNullOrEmpty(strBrandId))
                 {
                      //提示信息
                     e.Valid = false;
                     this.viewOrgTarget.HideEditor();
                     this.viewOrgTarget.ShowEditor();
                 }
                 else if (string.IsNullOrEmpty(strBranchId))
                 {
                      //提示信息
                     e.Valid = false;
                     this.viewOrgTarget.HideEditor();
                     this.viewOrgTarget.ShowEditor();
                 }
                 else if (string.IsNullOrEmpty(strCateId))
                 {
                      //提示信息
                     e.Valid = false;
                     this.viewOrgTarget.HideEditor();
                     this.viewOrgTarget.ShowEditor();
                 }
                 else if (string.IsNullOrEmpty(strOrgType))
                 {
                      //提示信息
                     e.Valid = false;
                     this.viewOrgTarget.HideEditor();
                     this.viewOrgTarget.ShowEditor();
                 }
                 else if (!string.IsNullOrEmpty(strOperDate))
                 {
                     int iRowCount = 0;
 
                     for (int i = 0; i < this.viewOrgTarget.RowCount; i++)
                     {
                         DataRow dr = this.viewOrgTarget.GetDataRow(i);
                         if (TWays.Utils.ToString(dr[DE.BmOrgTarget.BRAND_ID]) == strBrandId &&
                             TWays.Utils.ToString(dr[DE.BmOrgTarget.BRANCH_ID]) == strBranchId &&
                             TWays.Utils.ToString(dr[DE.BmOrgTarget.CATE_ID]) == strCateId &&
                             TWays.Utils.ToString(dr[DE.BmOrgTarget.ORG_TYPE]) == strOrgType &&
                             TWays.Utils.ToString(dr[DE.BmOrgTarget.OPER_DATE]) == strOperDate)
                         {
                             iRowCount++;
                         }
                     }
                     if (iRowCount > 0)
                     {
                          //提示信息
                         viewOrgTarget.SetFocusedRowCellValue(colShowOperDate, null);
                         e.Valid = false;
                         this.viewOrgTarget.HideEditor();
                         this.viewOrgTarget.ShowEditor();
                     }
                 }
             }
         }
验证编辑器事件(多列)

 

posted @ 2019-12-19 14:02  Haoeaoi  阅读(1382)  评论(0编辑  收藏  举报