随笔 - 833  文章 - 1  评论 - 106  阅读 - 200万

GridControl常见用法【转】

刚接触DevExpress第三方控件,把GridControl的常见用法整理一下,以供参考:

说明:

gcTest   GridControl   

gvText    GridView

            //隐藏最上面的GroupPanel  即去掉"Drag a Column Header Here To Group by that Column"         
            gvText.OptionsView.ShowGroupPanel = false;

            //修改最上面的GroupPanel
            gvText.GroupPanelText = "修改后的内容";

            //单元格不可编辑
            gvText.OptionsBehavior.Editable = false;

            //某列标题居中  0是列索引
            gvText.Columns[0].AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            //某列内容居中  0是列索引
            gvText.Columns[0].AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            //冻结列
            gvText.Columns[0].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;

            //自动改变行高适应内容
            gvText.OptionsView.RowAutoHeight = true;

            //显示自动筛选行
            gvText.OptionsView.ShowAutoFilterRow = false;

            //不显示字表信息
            gvText.OptionsView.ShowDetailButtons = false;
           
            //显示水平滚动条,自动列宽
             gvText.OptionsView.ColumnAutoWidth=false;

            //自动调整列宽
            gvText.BestFitColumns();

            //禁用GridControl中单击列弹出右键菜单
            gvText.OptionsMenu.EnableColumnMenu = false;

            //禁用GridControl中列头的过滤器
             gvText.OptionsCustomization.AllowFilter = false;

            //禁止各列头移动
              gvText.OptionsCustomization.AllowColumnMoving = false;

            //禁止各列头排序
              gvText.OptionsCustomization.AllowSort = false;

            //禁止各列头改变列宽
                gvText.OptionsCustomization.AllowColumnResizing = false;

            //根据绑定的数据源自动产生列,有时可以解决GridControl记录能获取而没有显示出来的问题
                 gvText.PopulateColumns();

            //奇偶行变色
            gvText.OptionsView.EnableAppearanceEvenRow = true;
            gvText.OptionsView.EnableAppearanceOddRow = true;
            gvText.Appearance.EvenRow.BackColor = Color.Gray;
            gvText.Appearance.OddRow.BackColor = Color.GreenYellow;


            //特殊列设置      日期
            string strDate="yyyy-MM-dd HH:mm";
            RepositoryItemDateEdit ride = new RepositoryItemDateEdit();
            ride.DisplayFormat.FormatString = strDate;
            ride.EditFormat.FormatString = strDate;
            ride.EditMask = strDate;
            gvText.Columns["日期"].ColumnEdit = ride;

            //列格式设置      decimal    价格  
            public const string MoneyFormatStr = "##,###,###,###,##0.00";
            RepositoryItemCalcEdit rice = new RepositoryItemCalcEdit();
            rice.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            rice.DisplayFormat.FormatString = MoneyFormatStr;            
            rice.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            rice.EditFormat.FormatString = MoneyFormatStr;
            rice.EditMask = MoneyFormatStr;
            gv.Columns["价格"].ColumnEdit = rice;

            //给单元格赋值
            gvText.SetRowCellValue(3, gvText.Columns["列名或列索引"],"要赋的值");
           
            //添加行
            gvText.AddNewRow();

            //添加列
            DevExpress.XtraGrid.Columns.GridColumn col = new DevExpress.XtraGrid.Columns.GridColumn();
            col.Caption = "列标题";
            col.FieldName = "列字段值";
            col.Visible = true;
            col.VisibleIndex = gvText.Columns.Count;
            gvText.Columns.Add(col);
        /// <summary>
        /// 获取选定行指定列单元格的值
        /// </summary>
        /// <param name="str">指定列的列名</param>
        /// <returns>单元格的值</returns>
        public string GetCellValue(string str) {
            int[] pRows = this.gvText.GetSelectedRows();
            if (pRows.GetLength(0) > 0){           
                return gvText.GetRowCellValue(pRows[0], str).ToString();
            }
            else {
                return null;
            }       
        }

posted on   3D入魔  阅读(2099)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2016-06-01 使用 ODP.NET 访问 Oracle(.net如何访问Oracle)详解【转】
2016-06-01 Web编程前端之7:web.config详解 【转】
2015-06-01 终于会用c#中的delegate(委托)和event(事件)了 [转]
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示