GridControl基础设置

private void FrmSearch_Load(object sender, EventArgs e)
        {
            //设置GridControl样式
            Common.GridControl.GridControlHelper.SetStyles(this.gridControl1.MainView as DevExpress.XtraGrid.Views.Base.ColumnView);
            //订阅行点击事件
            this.gridView1.RowClick += gridView1_RowClick;

        }

样式设置

public static void SetStyles(DevExpress.XtraGrid.Views.Base.ColumnView view)
        {
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                DevExpress.XtraGrid.Views.Grid.GridView gridView = view as DevExpress.XtraGrid.Views.Grid.GridView;

                gridView.OptionsView.ShowGroupPanel = false;                                              //隐藏最上面的GroupPanel
                gridView.OptionsView.ShowIndicator = false;                                               //隐藏指示列

                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;           //设置焦点框为整行
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;                                  //禁用单元格焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = true;                                    //启用整行焦点
                gridView.OptionsSelection.EnableAppearanceFocusedRow = true;                                    //启用整行焦点
                gridView.OptionsSelection.EnableAppearanceHideSelection = false;

                gridView.OptionsView.EnableAppearanceEvenRow = true;                                            //启用偶数行背景色
                gridView.OptionsView.EnableAppearanceOddRow = true;                                             //启用奇数行背景色

                //gridView.Appearance.EvenRow.BackColor = System.Drawing.Color.FromArgb(150, 237, 243, 254);      //设置偶数行背景色
                //gridView.Appearance.OddRow.BackColor = System.Drawing.Color.FromArgb(150, 199, 237, 204);       //设置奇数行背景色
                //gridView.Appearance.FocusedRow.BackColor = System.Drawing.Color.Red;
                //gridView.Appearance.SelectedRow.BackColor = System.Drawing.Color.Red;

            }

            //禁用自动生成列
            view.OptionsBehavior.AutoPopulateColumns = false;
            //禁用自动列宽
            if (view is DevExpress.XtraGrid.Views.Grid.GridView)
            {
                (view as DevExpress.XtraGrid.Views.Grid.GridView).OptionsView.ColumnAutoWidth = false;
            }
            //禁用数据过滤面板
            view.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;

            #region 添加列

            view.Columns.Clear();

            int index = 0;
            DevExpress.XtraGrid.Columns.GridColumn col = null;

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "DBName";
            col.Caption = "数据库名";
            col.Width = 200;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "ShortNodeText";
            col.Caption = "文件名";
            col.Width = 200;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "CreateDate";
            col.Caption = "创建日期";
            col.Width = 130;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "TaskFile";
            col.Caption = "任务文件";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "FullPath";
            col.Caption = "完整路径";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.FieldName = "Barcode";
            col.Caption = "电池条码";
            col.Width = 180;
            col.VisibleIndex = index++;
            view.Columns.Add(col);

            #endregion

            SetAllowEdit(view, false);                                          //禁用编辑
            SetAllowSort(view, DevExpress.Utils.DefaultBoolean.False);          //禁用排序
            SetAllowFilter(view, false);                                        //禁用数据过滤
        }
    gridView.OptionsView.ShowGroupPanel = false;//去除 gridView 头上的 "Drag a column header here to group by that column"
            gridControl1.AllowDrop = true;   //允许拖拽行
            gridView1.OptionsBehavior.Editable = true;//设置列能编辑
            列名称.OptionsColumn.AllowEdit = true;// 设置单元格能编辑        默认:true
            列名称.OptionsColumn.ReadOnly = true;//设置单元格只读并且不能拷贝  默认:false                                
            gridView1.OptionsBehavior.Editable = false;//不可编辑
            //只读
            gridView1.OptionsBehavior.ReadOnly = false;
            //可选择多行,使用Ctrl,Shift键等
            gridView1.OptionsSelection.MultiSelect = true;
            //允许单元格合并,当值相同时,则自动合并
            gridView1.OptionsView.AllowCellMerge = true;
            //列头自适应宽度
            gridView1.OptionsView.ColumnAutoWidth = true;
            //是否显示列头
            gridView1.OptionsView.ShowColumnHeaders = true;
            //是否显示脚
            gridView1.OptionsView.ShowFooter = true;
            //是否显示分组面板
            gridView1.OptionsView.ShowGroupPanel = true;
            //是否显示行号、指针
            gridView1.OptionsView.ShowIndicator = true;
            //列头可筛选
            gridView1.OptionsCustomization.AllowFilter = true;
            //可按列分组
            gridView1.OptionsCustomization.AllowGroup = true;
            //列头可排序
            gridView1.OptionsCustomization.AllowSort = true;
            //列头可移动顺序,拖动
            gridView1.OptionsCustomization.AllowColumnMoving = true;
            //列宽度调整,拖动宽度
            gridView1.OptionsCustomization.AllowColumnResizing = false;

 

禁用编辑

public static void SetAllowEdit(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsColumn.AllowEdit = isAllow;
            }
        }

禁用排序

public static void SetAllowSort(DevExpress.XtraGrid.Views.Base.ColumnView view, DevExpress.Utils.DefaultBoolean value)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsColumn.AllowSort = value;
            }
        }

禁用自带的过滤

 public static void SetAllowFilter(DevExpress.XtraGrid.Views.Base.ColumnView view, bool isAllow)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in view.Columns)
            {
                col.OptionsFilter.AllowAutoFilter = isAllow;
                col.OptionsFilter.AllowFilter = isAllow;
            }
        }

 

posted @ 2023-04-14 10:08  纡ゾ少︶ㄣ  阅读(64)  评论(0编辑  收藏  举报