1)去除 gridView 頭上的 "Drag a column header here to group by that column"ide
gridView.OptionsView.ShowGroupPanel = false;字體
2)顯示出 gridView 自帶的 搜索功能 this
gridView.OptionsFind.AlwaysVisible = true;spa
3)讓各列頭禁止移動3d
gridView.OptionsCustomization.AllowColumnMoving = false;code
4)讓各列頭禁止排序blog
gridView.OptionsCustomization.AllowSort = false;排序
5)禁止各列頭改變列寬事件
gridView.OptionsCustomization.AllowColumnResizing = false;string
6)設置gridView不可編輯
gridView.OptionsBehavior.Editable = false;
7)設置GridColumn(列)不可編輯
gridColumn.OptionsColumn.AllowEdit = false;
8)不顯示View的詳細信息。(不顯示每行前面的」+「)
gridView.OptionsDetail.EnableMasterViewMode = false;
9)判斷gridView是否選中了數據
int index= this.gridData.gridView.GetFocusedDataSourceRowIndex() ;
若是index小於0,證實沒有選中行,不然就選中了行數據
10)選中某一行
gridView.FocusedRowHandle =i;
gridView.SelectRow(i);
11)根據內容自適應列寬
gridView.BestFitColumns();
12)獲取選中行的數據
DataRow row = gridView.GetRow(i);
DataRow row =gridView.GetFocusedRow();
13)新增一行
gridView.AddNewRow()
14)刪除選中的行
gridView.DeleteSelectedRows();
15)設置某一列不可編輯
GridColumn.OptionsColumn.AllowEdit = false;
16)設置gridControl列寬自使用
gridView.OptionsView.ColumnAutoWidth = True;//爲false寬度不夠時出現水平滾動條
17)綁定數據
//綁定DataSet
gridControl.DataSource = DataSet;
gridControl.DataMember = "表名";
//綁定List集合
gcGumRecord.DataSource = new BindingList<類名>(集合);
1)設置成一次選擇一行,而且不能被編輯
this.gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
this.gridView.OptionsBehavior.Editable = false;
this.gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
2)設置gridView爲多選模式
gridView.OptionsSelection.MultiSelect = true;
gridView.OptionsSelection.CheckBoxSelectorColumnWidth = 30;
gridView.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
效果:
ps:不設置多選模式時:gridView.OptionsSelection.MultiSelect = false;
3)gridView爲多選模式時設置單選(不能同時勾選多個),使用SelectionChanged事件
private void GridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e) { var a = e.Action; if (gridView1.SelectedRowsCount > 1) { foreach (int rowHandle in gridView1.GetSelectedRows()) { if (rowHandle != e.ControllerRow) { gridView1.UnselectRow(rowHandle); } } } }
4)設置顯示gridView新增一行
gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
gridView.NewItemRowText = "點擊此處增長一行";
效果:
ps:僅設置顯示效果,實際能新增行要先給GridControl綁定數據源。
不設置新增一行時: gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
21)顯示行號
//Helper.SetGridViewShowRowNumber(gridView1); 直接調用便可 public static class Helper { /// <summary> /// 設置GridView顯示行號 /// </summary> /// <param name="gv"></param> public static void SetGridViewShowRowNumber(this DevExpress.XtraGrid.Views.Grid.GridView gv) { gv.OptionsView.ShowIndicator = true; ResetIndicatorWidth(gv); //設置行號 gv.CustomDrawRowIndicator += delegate (object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { DevExpress.Utils.Drawing.IndicatorObjectInfoArgs info = e.Info; if (info == null || !info.IsRowIndicator || e.RowHandle < 0) return; info.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; info.DisplayText = (e.RowHandle + 1).ToString(); }; //行數改變 gv.RowCountChanged += delegate (object sender, EventArgs e) { DevExpress.XtraGrid.Views.Grid.GridView selfView = sender as DevExpress.XtraGrid.Views.Grid.GridView; if (selfView == null) return; ResetIndicatorWidth(gv); }; //gv有子表時,展開子表重置其行號寬度 gv.MasterRowExpanded += delegate (object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e) { DevExpress.XtraGrid.Views.Grid.GridView View = sender as DevExpress.XtraGrid.Views.Grid.GridView; if (View == null) return; DevExpress.XtraGrid.Views.Grid.GridView dView = View.GetDetailView(e.RowHandle, e.RelationIndex) as DevExpress.XtraGrid.Views.Grid.GridView; if (dView == null) return; ResetIndicatorWidth(gv); }; } /// <summary> /// 重置行號寬度 /// </summary> /// <param name="gvw"></param> /// <param name="rowCount"></param> public static void ResetIndicatorWidth(this DevExpress.XtraGrid.Views.Grid.GridView gvw, int rowCount = -1) { gvw.IndicatorWidth = GetIndicatorWidth(rowCount == -1 ? gvw.RowCount : rowCount); } // 行號寬度 public static int GetIndicatorWidth(int p) { return 7 * p.ToString().Length + 20; } }
效果:
23)行色變化事件(RowCellStyle)
private void GridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) { int hand = e.RowHandle; if (hand < 0) return; e.Appearance.BackColor = Color.Yellow;// 改變行背景顏色 e.Appearance.ForeColor = Color.Red;// 改變行字體顏色 //e.Appearance.BackColor2 = Color.Blue;// 添加漸變顏色 }
效果:
gridView1.Columns.Clear(); //或者使用:gridView1.Columns.AddVisible("字段名","標題"); this.gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "A", Caption = "列一", VisibleIndex = 0}); //使用ColumnEdit屬性給列綁定控件 DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit lue_IsYes = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit(); this.gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() { FieldName = "B", Caption = "列二", VisibleIndex = 1, ColumnEdit = lue_IsYes });
效果:
使用循環每個單元格的思路進行數據驗證。
例如設置必填:
for (int i = 0; i < gridView1.RowCount; i++) { if (string.IsNullOrEmpty(gridView1.GetRowCellValue(i, "FieldName") as string)) { gridView1.FocusedRowHandle = i; var col = gridView1.Columns.Where(c => c.FieldName == "FieldName").FirstOrDefault(); gridView1.SetColumnError(col, "該字段不能爲空,請從新填寫!"); } }
效果: