DevExpress GridControl 后台设置列[通俗易懂]

DevExpress GridControl 后台设置列[通俗易懂]//////初始化GridView//////GridView名称///列标题///列字段///宽度如果宽度为0则列隐藏publicvoidInitGrid(DevExpress.XtraGrid.Views.Grid.GridVie

大家好,欢迎来到IT知识分享网。

  /// <summary>
        /// 初始化GridView 
        /// </summary>
        /// <param name="gv">GridView 名称</param>
        /// <param name="captions">列标题</param>
        /// <param name="fieldName">列字段</param>
        /// <param name="widths">宽度 如果宽度为0 则列隐藏</param>
        public void InitGrid(DevExpress.XtraGrid.Views.Grid.GridView gv,string[] captions, string[] fieldName, int[] widths) {
          //列标题 和列名个数是否匹配
            if (captions.Length != fieldName.Length) {
                MessageBox.Show("列标题和列名个数不匹配");
                return;
            }
            //清空列
            gv.Columns.Clear();
            //定义一列
            DevExpress.XtraGrid.Columns.GridColumn gc = null;
            //声明列数组
            DevExpress.XtraGrid.Columns.GridColumn[] gcs = new DevExpress.XtraGrid.Columns.GridColumn[captions.Length];
            //列设置
            for (int i = 0; i < captions.Length; i++) {
                gc = new DevExpress.XtraGrid.Columns.GridColumn();
                gc.Caption = captions[i];
                gc.FieldName = fieldName[i];
                gc.Name = fieldName[i];
                gc.VisibleIndex = i;
                //默认显示,如果宽度=0 ,则隐藏
                gc.Visible = true;
                if (widths != null)
                {
                    //如果列宽的个数与标题个数相同
                    if (widths.Length == captions.Length)
                    {
                        gc.Width = widths[i];
                        gc.Visible = (widths[i] != 0) ? true : false;
                    }
                }
                gcs.SetValue(gc, i);
            }
            //gv 增加列
            gv.Columns.AddRange(gcs);
            //列宽为空则自适应宽度
            gv.OptionsView.ColumnAutoWidth = (widths == null) ? true : false;

            //gv 数据列表设置
            gv.OptionsFind.AllowFindPanel = false;
            gv.Appearance.HeaderPanel.Options.UseTextOptions = true;
            gv.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            gv.OptionsView.ShowGroupPanel = false;

            //列宽为空
            if (widths == null) {
                gv.BestFitColumns();
            }
        }

调用(举例):

  //数据列表 加载
            InitGrid(gridView1,
                new string[]{"列标题1","列标题2","列标题3","列标题4"},
                new string[]{"字段1","字段2","字段3","字段4"},
                    new int[]{100,50,60,90});

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/25331.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信