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;
注:选中事件:SelectionChanged()、focusedRowChanged(),当选择的模式是单行时,须使用focusedRowChanged()
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.GridControl删除行
gridView1.DeleteSelectedRows();
33.GridControl定位到某行
//第一行 this.gridView1.MoveFirst(); //下一行 this.gridView1.MoveNext(); //最后一行 this.gridView1.MoveLast();
33.GridControl指定列内容显示居中
启用列的文本选项: 鼠标单击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.GridControl去掉指定列上的自动筛选功能
鼠标单击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.GridControl设置指定列左固定(冻结在左边)
列名称.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
36.GridControl获取指定列中的数据
string texts = gridView1.GetRowCellDisplayText(1, gridView1.Columns[3]);//获取第一行四列中的数据 string values = gridView1.GetRowCellValue(2, gridView1.Columns["列名称"]).ToString();//获取第二行指定列的数据
37.GridControl设置单元格数据(将0行4列的单元格赋值111)
gridView1.SetRowCellValue(0, gridView1.Columns[3], "111");//0:行号 gridView1.Columns[3]:列 111:赋值内容
38.GridControl设置获取焦点
//设置自定义行的焦点 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.GridControl自定义绘制表格颜色
//启用事件 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.GridControl根据动态条件自动义绘制指定行的颜色
//启用事件 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.GridControl自定义列显示文本
//启用事件 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"?"男":"女"; } } }
42.GridControl动态创建列并且设置一些基本的列属性
//创建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 }
43.GridControl清除排序等操作
this.GridView.SortInfo.ClearSorting();