C# DevExpress中GridControl控制元件的基本屬性設定和使用方法
1.GridControl隱藏GroupPanel(主面板)
隱藏:滑鼠單擊Run Designer-OptionsView-ShowGroupPanel=False;
修改:滑鼠單擊Run Designer-Appearance-GroupPanelText="檢索資訊";
this.gridView1.OptionsView.ShowGroupPanel = false;//隱藏 this.gridView1.GroupPanelText = "檢索資訊";
2.GridControl禁用標題列右鍵選單
滑鼠單擊Run Designer-OptionsMenu-EnableColumnMenu=False;
this.gridView1.OptionsMenu.EnableColumnMenu = false;//後臺程式碼實現
3.GridControl隱藏標題列資訊
滑鼠單擊Run Designer-OptionsView-ShowColumnHeaders=False;
this.gridView1.OptionsView.ShowColumnHeaders = false;//後臺程式碼實現
4.GridControl顯示最左邊的空白列並且設定它的寬度,把生成的序號在這個空白列中顯示出來
顯示左邊空白列:滑鼠單擊Run Designer-OptionsView-ShowIndicator=True;
設定寬度:滑鼠單擊Run Designer-Appearance-IndicatorWidth=50;
this.gridView1.OptionsView.ShowIndicator = true;// 顯示最左邊空白列 this.gridView1.IndicatorWidth = 50;//設定最左邊空白列的寬度 //啟用事件生成序號 this.gridView1.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(this.gridView1_CustomDrawRowIndicator);#region 生成序號 private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.Kind == DevExpress.Utils.Drawing.IndicatorKind.Header) { e.Info.DisplayText = "行號";//設定列名稱 } e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;//設定列居中顯示 if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } } #endregion
5.GridControl取消點選後的虛線框
修改:滑鼠單擊Run Designer-Appearance-FocusRectStyle=None;
this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;
6.GridControl禁用列寬自適應(總寬度超出桌面寬度顯示水平滾動條)
滑鼠單擊Run Designer-OptionsView-ColumnAutoWidth=False;
this.gridView1.OptionsView.ColumnAutoWidth = false;
7.GridControl禁用標題列過濾
滑鼠單擊Run Designer-OptionsCustomization-AllowFilter=False;
this.gridView1.OptionsCustomization.AllowFilter = false;
8.GridControl禁用標題列排序(點選標題列升序(降序))
滑鼠單擊Run Designer-OptionsCustomization-AllowSort=False;
this.gridView1.OptionsCustomization.AllowSort = false;
9.GridControll禁用標題列移動(拖動列改變列的位置)
滑鼠單擊Run Designer-OptionsCustomization-AllowColumnMoving=False;
this.gridView1.OptionsCustomization.AllowColumnMoving = false;
10.GridControll禁用標題列改變寬度(拖動列改變列的寬度)
滑鼠點選Run Designer-OptionsCustomization-AllowColumnResizing=False;
this.gridView1.OptionsCustomization.AllowColumnResizing = false;
11.GridControll禁用標題列分組
滑鼠單擊Run Designer-OptionsCustomization-AllowGroup=False;
this.gridView1.OptionsCustomization.AllowGroup = false;
12.GridControll是否顯示過濾面板
滑鼠單擊Run Designer-OptionsCustomization-AllowGroup=False;
this.gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
13.GridControll設定奇數行顏色為紅色
設定奇數行改變顏色:置滑鼠點選Run Designer-OptionsView-EnableAppearanceOddRow=True;
設定奇數行的顏色:滑鼠點選Run Designer-Appearance-OddRow-BackColor=System.Drawing.Color.Red;
this.gridView1.OptionsView.EnableAppearanceOddRow = true;//奇數行顏色變化 this.gridView1.Appearance.OddRow.BackColor = System.Drawing.Color.Red;//奇數行顏色設定
14.GridControll設定偶數行顏色為藍色
設定偶數行改變顏色:置滑鼠點選Run Designer-OptionsView-EnableAppearanceEvenRow=True;
設定偶數行的顏色:滑鼠點選Run Designer-Appearance-EvenRow-BackColor=System.Drawing.Color.Blue;
this.gridView1.OptionsView.EnableAppearanceEvenRow = true;//偶數行顏色變化 this.gridView1.Appearance.EvenRow.BackColor = System.Drawing.Color.Blue;//偶數行顏色設定
15.GridControll設定列中的資料只讀
設定編輯:滑鼠單擊Run Designer-OptionsBehavior-Editable=False;
this.gridView1.OptionsBehavior.Editable = false;
16.GridControll設定只能選擇行並且資料不能編輯
設定只能選中整行:滑鼠單擊Run Designer-OptionsBehavior-EditorShowMode=DevExpress.Utils.EditorShowMode.Click;
設定列的資料不能編輯:滑鼠單擊Run Designer-OptionsBehavior-Editable=False;
this.gridView1.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;//設定只能選中整行 this.gridView1.OptionsBehavior.Editable = false;//設定列的資料不能編輯
17.GridControll設定列中的資料只讀並且不可拷貝
設定資料可編輯:滑鼠單擊Run Designer-OptionsBehavior-Editable=True;
設定資料只讀並且不可拷貝:滑鼠單擊Run Designer-OptionsBehavior-ReadOnly=True;
gridView1.OptionsBehavior.Editable = true;//設定列能編輯 gridView1.OptionsBehavior.ReadOnly = true;//設定設定只讀並且不可拷貝
18.GridControll設定指定列中的資料只讀不可拷貝
設定列能編輯: 滑鼠單擊Run Designer-OptionsBehavior-Editable=False;
設定單元格能編輯: 滑鼠單擊Run Designer-Columns- 列名稱-OptionsColumn-AllowEdit=True;
設定單元格只讀並且不能拷貝: 滑鼠單擊Run Designer-Columns- 列名稱-OptionsColumn-ReadOnly=True;
gridView1.OptionsBehavior.Editable = true;//設定列能編輯 列名稱.OptionsColumn.AllowEdit = true;// 設定單元格能編輯 預設:true 列名稱.OptionsColumn.ReadOnly = true;//設定單元格只讀並且不能拷貝 預設:false
19.GridControll設定只能選中整行並且列中的資料不能編輯並且選中列的顏色跟著改變
設定只能選中整行:滑鼠單擊Run Designer-OptionsBehavior-EditorShowMode=DevExpress.Utils.EditorShowMode.Click;
設定列的資料不能編輯:滑鼠單擊Run Designer-OptionsBehavior-Editable=False;
設定選中列的顏色跟著改變:滑鼠單擊Run Designer-OptionsSelection-EnableAppearanceFocusedCell=True;
this.gridView1.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;//設定只能選中整行 this.gridView1.OptionsBehavior.Editable = false;//設定列的資料不能編輯 this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;//選中列的顏色跟著改變
20.GridControll禁用自動填充列
滑鼠單擊Run Designer-OptionsBehavior-AutoPopulateColumns=False;
this.gridView1.OptionsBehavior.AutoPopulateColumns = false;
21.GridControll選中整行
滑鼠單擊Run Designer-OptionsSelection-EnableAppearanceFocusedRow=True;
this.gridView1.OptionsSelection.EnableAppearanceFocusedRow = true;
22.GridControll啟用列的多選,列的選擇方式(CellSelect:多選單元格 CheckBoxRowSelect:複選框 RowSelect:多選行)
啟用列的多選:滑鼠單擊Run Designer-OptionsSelection-MultiSelect=True;
設定列為複選框多選:滑鼠單擊Run Designer-OptionsSelection-MultiSelectMode=DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
this.gridView1.OptionsSelection.MultiSelect = true; this.gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
23.GridControll設定標題列居中
滑鼠單擊Run Designer-Appearance-HeaderPanel-TextOptions-HAlignment=DevExpress.Utils.HorzAlignment.Center;
this.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
24.GridControll不使用TAB/SHIFT+TAB移動焦點
滑鼠單擊Run Designer-OptionsNavigation-UseTabKey=True;
gridView1.OptionsNavigation.UseTabKey = true;
25.GridControll顯示合計
滑鼠單擊Run Designer-OptionsView-ShowFooter=True;
設定指定列的合計:Run Designer-Columns-選擇列名稱-SummaryItem-SummaryType=Sum;
this.gridView1.OptionsView.ShowFooter = true; //動態建立GridColumn列的時候設定 #region 第一種方式 //age:列名稱 age.SummaryItem.DisplayFormat = "{0:f2}";//設定顯示格式 age.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;//彙總方式 #endregion #region 第二種方式 //age:列名稱 DevExpress.Data.SummaryItemType.Sum:彙總方式 {0:n2}:顯示格式 age.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] { new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Sum, "age", "{0:n2}")}); #endregion
26.GridControll設定列中的資料自動換行
滑鼠單擊Run Designer-OptionsView-RowAutoHeight=True;
給指定列設定控制元件:Run Designer-Columns-選擇列名稱-ColumnEdit選擇“MemoEdit”;
this.gridView1.OptionsView.RowAutoHeight = true;//設定自動換行 #region 建立一個文字控制元件 DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit memoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit(); memoEdit.AutoHeight = false; memoEdit.Name = "memoEdit"; #endregion school.ColumnEdit = memoEdit;//school:表示列 在建立GridColumn列的時候設定ColumnEdit的值
27.GridControll設定相同列合併
滑鼠單擊Run Designer-OptionsView-AllowCellMerge=True;
this.gridView1.OptionsView.AllowCellMerge = true;//啟用合併列
// 啟用指定合併列事件 this.gridView1.CellMerge += new DevExpress.XtraGrid.Views.Grid.CellMergeEventHandler(gridView1_CellMerge); #region 合併指定的列 private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e) { int rowHandle1 = e.RowHandle1;//合併列的第一行 int rowHandle2 = e.RowHandle2;//合併列從1開始到最後行就是 string strValue1 = this.gridView1.GetListSourceRowCellValue(rowHandle1, "age").ToString();//age:列名稱 string strValue2 = this.gridView1.GetListSourceRowCellValue(rowHandle2, "age").ToString();//age:列名稱 if (strValue1 != strValue2) { e.Merge = false; //值相同的2個單元格是否要合併在一起 e.Handled = true; //合併單元格是否已經處理過,無需再次進行省缺處理 } } #endregion
28.GridControll顯示自帶的 搜尋功能
滑鼠單擊Run Designer-OptionsFind-AlwaysVisible=True;
this.gridView1.OptionsFind.AlwaysVisible = true;
29.GridControll控制元件繫結資料來源
gridControl1.DataSource = dt;//dt 資料來源可以是DataTable也可以是List集合
30.GridControll控制元件沒有繫結資料來源或者查詢出來的資料為0條記錄時顯示自定義的提示語句
//啟用事件 this.gridView1.CustomDrawEmptyForeground += new DevExpress.XtraGrid.Views.Base.CustomDrawEventHandler(this.gridView1_CustomDrawEmptyForeground); //具體實現的提示語句 private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e) { DevExpress.XtraGrid.Views.Base.ColumnView columnView = sender as DevExpress.XtraGrid.Views.Base.ColumnView; BindingSource bindingSource = this.gridView1.DataSource as BindingSource; if (bindingSource == null || bindingSource.Count == 0) { string str = "沒有查詢到你所想要的資料!"; Font f = new Font("宋體", 10, FontStyle.Bold); Rectangle r = new Rectangle(e.Bounds.Top + 105, e.Bounds.Left + 5, e.Bounds.Right - 5, e.Bounds.Height - 5); e.Graphics.DrawString(str, f, Brushes.Black, r); } }
31.GridControll新增新行
新增行: gridView1.AddNewRow();
實現 gridView_InitNewRow 事件(對於新加行的特別的設定)
//啟用事件 this.gridView1.InitNewRow += new DevExpress.XtraGrid.Views.Grid.InitNewRowEventHandler(gridView1_InitNewRow); //新增行按鈕事件 private void btn_addRow_Click(object sender, EventArgs e) { //注:在新增行的時候必須先給“gridControl1.DataSource”繫結資料來源不然新增會沒有任何反映 gridView1.AddNewRow(); } //新增行 private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) { #region 根據列名新增資料:“name”列名稱-值 “王大” //DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView; //view.SetRowCellValue(e.RowHandle, view.Columns["name"], "王大"); //view.SetRowCellValue(e.RowHandle, view.Columns["age"], "20"); #endregion #region 複製最後一行 DevExpress.XtraGrid.Views.Base.ColumnView View = sender as DevExpress.XtraGrid.Views.Base.ColumnView; View.SetRowCellValue(e.RowHandle, View.Columns[0], gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount - 2), gridView1.Columns[0])); //複製最後一行的資料到新行 View.SetRowCellValue(e.RowHandle, View.Columns[1], gridView1.GetRowCellValue(gridView1.GetRowHandle(gridView1.RowCount - 2), gridView1.Columns[1])); //複製最後一行的資料到新行 #endregion #region 只新增一行,沒有如何資料 //Application.DoEvents(); //try //{ // this.gridView1.BeginInit(); // this.gridControl1.Focus();//獲取游標 // this.gridView1.Focus();//獲取游標 // this.gridView1.SelectCell(e.RowHandle, this.gridView1.Columns["列名稱"]);//選中列 // this.gridView1.FocusedColumn = this.gridView1.Columns["列名稱"];//設定焦點行的焦點單元格的位置 // this.gridView1.ShowEditor();//設定編輯狀態的列 //} //finally //{ // this.gridView1.EndInit(); //} #endregion }
32.GridControll刪除行
gridView1.DeleteSelectedRows();
33.GridControll定位到某行
//第一行 this.gridView1.MoveFirst(); //下一行 this.gridView1.MoveNext(); //最後一行 this.gridView1.MoveLast();
33.GridControll指定列內容顯示居中
啟用列的文字選項: 滑鼠單擊Run Designer-Columns- 列名稱-Appearance-AppearanceCell-Options-UseTextOptions=True;
設定列的文字居中: 滑鼠單擊Run Designer-Columns- 列名稱-Appearance-AppearanceCell-TextOptions-HAlignment=Center;
列名稱.AppearanceCell.Options.UseTextOptions = true; 列名稱.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
34.GridControll去掉指定列上的自動篩選功能
滑鼠單擊Run Designer-Columns- 列名稱-Options-OptionsFilter-AllowAutoFilter=False;
滑鼠單擊Run Designer-Columns- 列名稱-Options-OptionsFilter-AllowFilter=False;
滑鼠單擊Run Designer-Columns- 列名稱-Options-OptionsFilter-ImmediateUpdateAutoFilter=False;
列名稱.OptionsFilter.AllowAutoFilter = false; 列名稱.OptionsFilter.AllowFilter = false; 列名稱.OptionsFilter.ImmediateUpdateAutoFilter = false;
35.GridControll設定指定列左固定(凍結在左邊)
列名稱.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
36.GridControll獲取指定列中的資料
string texts = gridView1.GetRowCellDisplayText(1, gridView1.Columns[3]);//獲取第一行四列中的資料 string values = gridView1.GetRowCellValue(2, gridView1.Columns["列名稱"]).ToString();//獲取第二行指定列的資料
37.GridControll設定單元格資料(將0行4列的單元格賦值111)
gridView1.SetRowCellValue(0, gridView1.Columns[3], "111");//0:行號 gridView1.Columns[3]:列 111:賦值內容
38.GridControll設定獲取焦點
//設定自定義行的焦點 this.gridView1.FocusedRowHandle = 3; //獲取焦點行的下標 int lineNumber = this.gridView1.FocusedRowHandle; //獲取焦點行的下標 DevExpress.XtraGrid.Views.Base.ColumnView newview = (DevExpress.XtraGrid.Views.Base.ColumnView)gridControl1.FocusedView; lineNumber = newview.FocusedRowHandle; //根據列名稱獲取焦點行中任意單元格的值 DevExpress.XtraGrid.Views.Base.ColumnView cv = (DevExpress.XtraGrid.Views.Base.ColumnView)gridControl1.FocusedView; int focusedhandle = cv.FocusedRowHandle; object rowIdObj = gridView1.GetRowCellValue(focusedhandle, "age");//“age”表示:列名稱 if (DBNull.Value != rowIdObj) { int age = Convert.ToInt32(rowIdObj); } //根據列名稱設定焦點行的焦點單元格 DevExpress.XtraGrid.Views.Base.ColumnView view = (DevExpress.XtraGrid.Views.Base.ColumnView)gridControl1.FocusedView; view.FocusedColumn = view.Columns["classs"];//“classs”表示:列名稱
39.GridControll自定義繪製表格顏色
//啟用事件 this.gridView1.CustomDrawCell += new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(this.gridView1_CustomDrawCell); private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { if (e.Column.FieldName.Equals("age"))//列名稱 { DevExpress.XtraGrid.Views.Grid.ViewInfo.GridCellInfo cellInfo = e.Cell as DevExpress.XtraGrid.Views.Grid.ViewInfo.GridCellInfo; if (cellInfo.IsDataCell) { if (double.Parse(cellInfo.CellValue.ToString()) < 20)//年齡小於20 { e.Appearance.BackColor = Color.OrangeRed; } else if (double.Parse(cellInfo.CellValue.ToString()) < 100)//年齡小於100 { e.Appearance.BackColor = Color.YellowGreen; } else { e.Appearance.BackColor = Color.Gold; } } } }
40.GridControll根據動態條件自動義繪製指定行的顏色
//啟用事件 this.gridView1.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(this.gridView1_RowStyle); private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { DevExpress.XtraGrid.Views.Grid.GridView View = sender as GridView; if (e.RowHandle >= 0) { object needAlert = View.GetRowCellValue(e.RowHandle, View.Columns["age"]); if (needAlert != null & needAlert != DBNull.Value && needAlert.ToString().Trim() != "0" ) { decimal AverValue = Convert.ToDecimal(View.GetRowCellValue(e.RowHandle, View.Columns["age"])); if ( AverValue > 50) { e.Appearance.ForeColor = Color.Red; e.Appearance.BackColor = Color.LightGray; } } } }
41.GridControll自定義列顯示文字
//啟用事件 this.gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(this.gridView1_CustomColumnDisplayText); private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { if (e.Value != null && e.Value.ToString().Length > 0) { if (e.Column.FieldName == "gender") { e.DisplayText = e.Value.ToString().Trim()=="1"?"男":"女"; } } }
41.動態建立列並且設定一些基本的列屬性
//建立DataTable賦值並給gridControl指定資料來源 private void CreateData() { DataTable dt = new DataTable(); dt.Columns.Add("name", Type.GetType("System.String")); dt.Columns.Add("age", Type.GetType("System.String")); dt.Columns.Add("gender", Type.GetType("System.String")); dt.Columns.Add("school", Type.GetType("System.String")); dt.Columns.Add("classs", Type.GetType("System.String")); for (int i = 0; i < 5; i++) { DataRow row = dt.NewRow(); row["name"] = "張三" + i; if (i == 0 || i == 1) row["age"] = "18"; else row["age"] = "18" + i; row["gender"] = "1"; row["school"] = "理工大學理工大學理工大學理工大學理工大學理工大學理工大學理工大學理工大學理工大學理工大學理工大學" + i; row["classs"] = "天才2021級" + i; dt.Rows.InsertAt(row, i); } gridControl1.DataSource = dt;//表格獲取資料來源 } //建立列、表格中控制元件的建立和列的一些屬性設定 private void CreateColumn() { #region 建立一個文字控制元件 DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit memoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit(); memoEdit.AutoHeight = false; memoEdit.Name = "memoEdit"; #endregion
#region 設定列的屬性 GridColumn name = new GridColumn(); name.Caption = " 姓名";//顯示名稱 name.FieldName = "name";//資料來源名稱 name.Width = 50;//寬度 name.Visible = true;//是否顯示 name.OptionsColumn.AllowEdit = true;// 單元格能編輯 name.OptionsColumn.ReadOnly = false;//設定只讀 name.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;//把本列固定在左邊 this.gridView1.Columns.Add(name);//給指定的“GridBand”("基本資訊")新增列 GridColumn age = new GridColumn(); age.Caption = "年齡"; age.FieldName = "age"; age.Width = 45; age.Visible = true; age.AppearanceCell.Options.UseTextOptions = true; age.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; age.SummaryItem.DisplayFormat = "{0:f2}";//設定顯示格式 age.SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Sum;//彙總方式 this.gridView1.Columns.Add(age); GridColumn gender = new GridColumn(); gender.Caption = "性別"; gender.AppearanceCell.Options.UseTextOptions = true;//啟動設定文字顯示 gender.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;//設定文字居中 gender.OptionsFilter.AllowAutoFilter = false;//設定列不能篩選 gender.OptionsFilter.AllowFilter = false;//設定列不能篩選 gender.OptionsFilter.ImmediateUpdateAutoFilter = false;//設定列不能篩選 gender.FieldName = "gender"; gender.MinWidth = 45; gender.Visible = true; this.gridView1.Columns.Add(gender); GridColumn school = new GridColumn(); school.Caption = "學校"; school.FieldName = "school"; school.ColumnEdit = memoEdit; school.MinWidth = 100; school.Visible = true; this.gridView1.Columns.Add(school); GridColumn classs = new GridColumn(); classs.Caption = "班級"; classs.FieldName = "classs"; classs.MinWidth = 80; classs.Visible = true; this.gridView1.Columns.Add(classs); #endregion }
搜尋
複製