private void rILinkEditInfoDel_Click(object sender, EventArgs e) { if (XtraMessageBox.Show("請肯定是否刪除當前記錄?", "警告",MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { DataRow row =gvInfos.GetDataRow(gvInfos.FocusedRowHandle); delByCode(row["Code"].ToString()); XtraMessageBox.Show("操做成功!"); } }
Hashtable ht = new Hashtable(); private void gridView6_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgse) { GridView View = sender as GridView; if (e.RowHandle >= 0) { object needAlert =View.GetRowCellValue(e.RowHandle, View.Columns["needAlert"]); if (needAlert != null &needAlert != DBNull.Value && needAlert.ToString().Trim() !="0" & View.GetRowCellValue(e.RowHandle,View.Columns["Value"]) != DBNull.Value) { decimal AverValue = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle,View.Columns["Value"])); objectMinValue = View.GetRowCellValue(e.RowHandle,View.Columns["MinValue"]); objectMaxVlaue = View.GetRowCellValue(e.RowHandle,View.Columns["MaxValue"]); if(MinValue != DBNull.Value & MinValue != null & MaxVlaue.ToString() !="" & MaxVlaue != DBNull.Value && MaxVlaue != null &MaxVlaue.ToString() != "") { decimal gridColumn2 = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle,View.Columns["MinValue"])); decimal gridColumn1 = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle,View.Columns["MaxValue"])); if (gridColumn2 > AverValue || AverValue > gridColumn1) { if (!ht.ContainsKey("pic")) ht.Add("pic", GetImage(1)); e.Value = ht["pic"]; } } } } } /// <summary> /// 由資源文件獲取圖片 /// </summary> /// <param name="key"></param> /// <returns></returns> byte[] GetImage(int key) { Image img =DevExpress.Utils.Controls.ImageHelper.CreateImageFromResources(string.Format("RiverSys.Resources.{0}.gif",key.ToString()), typeof(RiverInfos).Assembly); returnDevExpress.XtraEditors.Controls.ByteImageConverter.ToByteArray(img,ImageFormat.Gif); } /// <summary> /// 動態根據條件設置行樣式 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gridView6_RowStyle(object sender,DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { GridView View = sender as GridView; if (e.RowHandle >= 0) { object needAlert =View.GetRowCellValue(e.RowHandle, View.Columns["needAlert"]); if (needAlert != null &needAlert != DBNull.Value && needAlert.ToString().Trim() !="0" & View.GetRowCellValue(e.RowHandle,View.Columns["Value"]) != DBNull.Value) { decimal AverValue = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["Value"])); objectMinValue = View.GetRowCellValue(e.RowHandle,View.Columns["MinValue"]); objectMaxVlaue = View.GetRowCellValue(e.RowHandle,View.Columns["MaxValue"]); if(MinValue != DBNull.Value & MinValue != null & MaxVlaue.ToString() !="" & MaxVlaue != DBNull.Value && MaxVlaue != null &MaxVlaue.ToString() != "") { decimal gridColumn2 = Convert.ToDecimal(MinValue); decimal gridColumn1 = Convert.ToDecimal(MaxVlaue); if (gridColumn2 > AverValue || AverValue > gridColumn1) { e.Appearance.ForeColor = Color.Red; e.Appearance.BackColor = Color.LightGray; } } } } }
private void gvMapColor_CustomUnboundColumnData(object sender,DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view = sender as GridView; DataView dv = view.DataSource as DataView; if (e.IsGetData) { string strVal =dv[e.ListSourceRowIndex]["Color"].ToString(); if (strVal != "") { //e.Value = DevExpress.Utils.StyleLayout.ColorFromString(strVal); e.Value = Common.HexToColor(strVal); } } else { //Color colorVal =DevExpress.Utils.StyleLayout.ColorFromString(e.Value.ToString()); Color colorVal =(Color)e.Value; dv[e.ListSourceRowIndex]["Color"] =Common.RGB_HEX(colorVal.ToArgb()); } }
private void gvMapColor_CustomUnboundColumnData(object sender,DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view = sender as GridView; DataView dv = view.DataSource as DataView; if (e.IsGetData) { string strVal =dv[e.ListSourceRowIndex]["Color"].ToString(); if (strVal != "") { //e.Value = DevExpress.Utils.StyleLayout.ColorFromString(strVal); e.Value = Common.HexToColor(strVal); } } else { //Color colorVal =DevExpress.Utils.StyleLayout.ColorFromString(e.Value.ToString()); Color colorVal =(Color)e.Value; dv[e.ListSourceRowIndex]["Color"] =Common.RGB_HEX(colorVal.ToArgb()); } } 4、關於 GridControl 驗證示例 /**//// <summary> /// 初始化GridView,綁定數據 /// </summary> /// <param name="parentId"></param> private void GridViewBindData(string parentId) { this.gridView1.Columns.Clear(); this.FDs= areaSetupActionHelper.getDsRegionByParentId(parentId); this.gridCArea.DataSource =this.FDs.Tables[0].DefaultView; this.gridView1.Columns["id"].VisibleIndex =-1; this.gridView1.Columns["childCounts"].VisibleIndex= -1; this.gridView1.Columns["reg_id"].Caption ="區劃編號"; this.gridView1.Columns["reg_name"].Caption ="區劃名稱"; this.gridView1.Columns["parent_id"].Caption ="父區劃編號"; this.gridView1.Columns["reg_desc"].Caption ="區劃描述"; this.gridView1.Columns["parent_id"].ImageIndex =1; this.gridView1.Columns["reg_desc"].ImageIndex = 0; RepositoryItemTextEdit textEditReg_Id = newRepositoryItemTextEdit(); textEditReg_Id.Mask.EditMask =parentId+"\\d{2,3}"; textEditReg_Id.Mask.MaskType =DevExpress.XtraEditors.Mask.MaskType.Regular; this.gridView1.Columns["reg_id"].ColumnEdit =textEditReg_Id; this.gridView1.Columns["reg_desc"].ColumnEdit= new RepositoryItemMemoExEdit(); TreeListNode node =this.treelArea.FocusedNode.ParentNode; string fid =node==null?"0":node.GetValue("RegID").ToString().Trim(); DataSet ds =areaSetupActionHelper.getDsRegionByParentId(fid); RepositoryItemLookUpEdit lookUEParent_Id = newRepositoryItemLookUpEdit(); lookUEParent_Id.Columns.Add(newLookUpColumnInfo("reg_id", 40, "區劃編號")); lookUEParent_Id.Columns.Add(newLookUpColumnInfo("reg_name", 40, "區劃名稱")); lookUEParent_Id.DataSource = ds.Tables[0].DefaultView; lookUEParent_Id.ValueMember = "reg_id"; lookUEParent_Id.DisplayMember = "reg_id"; this.gridView1.Columns["parent_id"].ColumnEdit =lookUEParent_Id; } /**//// <summary> /// gridView單元格驗證的相關處理程序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void gridView1_ValidatingEditor(objectsender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e) { if (e.Valid == false&this.gridView1.FocusedColumn.FieldName =="reg_id") { e.ErrorText = "區劃編號不合法!\n應爲父區劃編號加2~3位數據組成!"; } if (this.gridView1.FocusedColumn.FieldName =="reg_name") { Regex reg=new Regex(@"[\u4e00-\u9fa5]{1,20}"); Match m=reg.Match(e.Value.ToString().Trim()); if (m.Length != e.Value.ToString().Trim().Length) { e.Valid = false; e.ErrorText = "區劃名稱應爲漢字\n長度爲1至20"; } } } private void gridView1_InvalidValueException(objectsender, InvalidValueExceptionEventArgs e) { if (MyDialog.Alert(" 您所填寫的內容不符合規則\n 要放棄您剛纔對此項所作的更改嗎?", "您所編輯的內容不符合規則", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) ==DialogResult.Yes) { e.ExceptionMode = ExceptionMode.Ignore; } } /**//// <summary> /// gridView行驗證的相關處理程序 /// </summary> private void gridView1_ValidateRow(objectsender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e) { string regid = this.gridView1.GetRowCellValue(e.RowHandle,"reg_id").ToString().Trim(); string regName = this.gridView1.GetRowCellValue(e.RowHandle,"reg_name").ToString().Trim(); if ( regid.Length < 1) { e.Valid = false; this.gridView1.SetColumnError(this.gridView1.Columns["reg_id"],"請填寫區劃編號!",DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default); // e.ErrorText = "區劃名稱不能爲空!"; } if (regName.Length < 1) { e.Valid = false; this.gridView1.SetColumnError(this.gridView1.Columns["reg_name"],"區劃名稱不能爲空!",DevExpress.XtraEditors.DXErrorProvider.ErrorType.Default); } } private void gridView1_InvalidRowException(object sender,DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e) { if (e.RowHandle >= 0) { if (this.gridView1.GetRowCellValue(e.RowHandle,this.gridView1.Columns["reg_id"]).ToString().Trim() == ""|| this.gridView1.GetRowCellValue(e.RowHandle,this.gridView1.Columns["reg_name"]).ToString().Trim() =="") { if (MyDialog.Alert(" 您所填寫的內容不符合規則\n 要放棄您剛纔對此項所作的更改嗎?","您所編輯的內容不符合規則", MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes) { e.ExceptionMode = ExceptionMode.Ignore; } else { e.ExceptionMode = ExceptionMode.NoAction; } } } else { e.ExceptionMode = ExceptionMode.Ignore; } }
本文來源:慧都學院-企業級IT培訓平臺html