DevExpress中GridControl控件的基本属性设置和使用方法

目录

1.GridControl隐藏GroupPanel(主面板)

2.GridControl禁用标题列右键菜单

3.GridControl隐藏标题列信息

4.GridControl显示最左边的空白列并且设置它的宽度,把生成的序号在这个空白列中显示出来 

5.GridControl取消点击后的虚线框

6.GridControl禁用列宽自适应(总宽度超出桌面宽度显示水平滚动条)

7.GridControl禁用标题列过滤

8.GridControl禁用标题列排序(点击标题列升序(降序)) 

9.GridControl禁用标题列移动(拖动列改变列的位置) 

10.GridControl禁用标题列改变宽度(拖动列改变列的宽度)

11.GridControl禁用标题列分组

12.GridControl是否显示过滤面板 

13.GridControl设置奇数行颜色为红色

14.GridControl设置偶数行颜色为蓝色

15.GridControl设置列中的数据只读

17.GridControl设置列中的数据只读并且不可拷贝

18.GridControl设置指定列中的数据只读不可拷贝

19.GridControl设置只能选中整行并且列中的数据不能编辑并且选中列的颜色跟着改变

20.GridControl禁用自动填充列

21.GridControll选中整行

22.GridControl启用列的多选,列的选择方式(CellSelect:多选单元格 CheckBoxRowSelect:复选框 RowSelect:多选行)

23.GridControl设置标题列居中

24.GridControl不使用TAB/SHIFT+TAB移动焦点

25.GridControl显示合计

26.GridControl设置列中的数据自动换行

27.GridControl设置相同列合并

28.GridControl显示自带的 搜索功能 

29.GridControl控件绑定数据源

30.GridControl控件没有绑定数据源或者查询出来的数据为0条记录时显示自定义的提示语句

31.GridControl添加新行

32.GridControl删除行

33.GridControl定位到某行

34.GridControl指定列内容显示居中

35.GridControl去掉指定列上的自动筛选功能

36.GridControl设置指定列左固定(冻结在左边)

37.GridControl获取指定列中的数据

38.GridControll设置单元格数据(将0行4列的单元格赋值111)

39.GridControl设置获取焦点

40.GridControl自定义绘制表格颜色

41.GridControl根据动态条件自动义绘制指定行的颜色

42.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.GridControl禁用标题列移动(拖动列改变列的位置) 

鼠标单击Run Designer-OptionsCustomization-AllowColumnMoving=False;

this.gridView1.OptionsCustomization.AllowColumnMoving = false;

10.GridControl禁用标题列改变宽度(拖动列改变列的宽度)

鼠标点击Run Designer-OptionsCustomization-AllowColumnResizing=False;

this.gridView1.OptionsCustomization.AllowColumnResizing = false;

11.GridControl禁用标题列分组

鼠标单击Run Designer-OptionsCustomization-AllowGroup=False; 

this.gridView1.OptionsCustomization.AllowGroup = false;

12.GridControl是否显示过滤面板 

 鼠标单击Run Designer-OptionsCustomization-AllowGroup=False;

this.gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;

13.GridControl设置奇数行颜色为红色

设置奇数行改变颜色:置鼠标点击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.GridControl设置偶数行颜色为蓝色

设置偶数行改变颜色:置鼠标点击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.GridControl设置列中的数据只读

设置编辑:鼠标单击Run Designer-OptionsBehavior-Editable=False;

this.gridView1.OptionsBehavior.Editable = false;

16.GridControl设置只能选择行并且数据不能编辑

设置只能选中整行:鼠标单击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.GridControl设置列中的数据只读并且不可拷贝

设置数据可编辑:鼠标单击Run Designer-OptionsBehavior-Editable=True;

设置数据只读并且不可拷贝:鼠标单击Run Designer-OptionsBehavior-ReadOnly=True;

gridView1.OptionsBehavior.Editable = true;//设置列能编辑
gridView1.OptionsBehavior.ReadOnly = true;//设置设置只读并且不可拷贝 

18.GridControl设置指定列中的数据只读不可拷贝

设置列能编辑: 鼠标单击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.GridControl设置只能选中整行并且列中的数据不能编辑并且选中列的颜色跟着改变

设置只能选中整行:鼠标单击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.GridControl禁用自动填充列

鼠标单击Run Designer-OptionsBehavior-AutoPopulateColumns=False;

this.gridView1.OptionsBehavior.AutoPopulateColumns = false;

21.GridControll选中整行

鼠标单击Run Designer-OptionsSelection-EnableAppearanceFocusedRow=True;

this.gridView1.OptionsSelection.EnableAppearanceFocusedRow = true;

22.GridControl启用列的多选,列的选择方式(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.GridControl设置标题列居中

鼠标单击Run Designer-Appearance-HeaderPanel-TextOptions-HAlignment=DevExpress.Utils.HorzAlignment.Center;

this.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

24.GridControl不使用TAB/SHIFT+TAB移动焦点

鼠标单击Run Designer-OptionsNavigation-UseTabKey=True;

gridView1.OptionsNavigation.UseTabKey = true;

25.GridControl显示合计

鼠标单击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.GridControl设置列中的数据自动换行

鼠标单击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.GridControl设置相同列合并

鼠标单击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.GridControl显示自带的 搜索功能 

鼠标单击Run Designer-OptionsFind-AlwaysVisible=True;

this.gridView1.OptionsFind.AlwaysVisible = true;

29.GridControl控件绑定数据源

gridControl1.DataSource = dt;//dt 数据源可以是DataTable也可以是List集合

30.GridControl控件没有绑定数据源或者查询出来的数据为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.GridControl添加新行

新增行: 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();

34.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;

35.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;

36.GridControl设置指定列左固定(冻结在左边)

列名称.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;

37.GridControl获取指定列中的数据

string texts = gridView1.GetRowCellDisplayText(1, gridView1.Columns[3]);//获取第一行四列中的数据
string values = gridView1.GetRowCellValue(2, gridView1.Columns["列名称"]).ToString();//获取第二行指定列的数据

38.GridControll设置单元格数据(将0行4列的单元格赋值111)

gridView1.SetRowCellValue(0, gridView1.Columns[3], "111");//0:行号 gridView1.Columns[3]:列  111:赋值内容

39.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”表示:列名称

40.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;
                    }
                }
            }
        }

41.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;
                        }
                }
            }
        }

42.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"?"男":"女";
           }
      }
}

  • 7
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值