开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 6426|回复: 5

GridControl 自定义显示列

[复制链接]

0

精华

100

贡献

33

赞扬

帖子
132
软币
3320
在线时间
53 小时
注册时间
2013-8-31
发表于 2013-8-31 19:02:20 | 显示全部楼层 |阅读模式
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Nbjjy.Utilities
{
    public partial class CustomColumnDialog : DevExpress.XtraEditors.XtraForm
    {
        private DataTable dt;
        private GridColumnCollection cols;
        public CustomColumnDialog(GridColumnCollection cols)
        {
            InitializeComponent();
            this.cols = cols;
            this.GridColumn();
            this.gridControl1.DataSource = this.dt;
        }
        private void GridColumn()
        {
            this.dt = new DataTable();
            this.dt.Columns.AddRange(new DataColumn[]
            {
                new DataColumn("Selected", typeof(bool)),
                new DataColumn("Caption", typeof(string))
            });
            foreach (GridColumn gridColumn in cols)
            {
                DataRow dataRow = this.dt.NewRow();
                dataRow["Selected"] = (gridColumn.VisibleIndex >= 0);
                dataRow["Caption"] = gridColumn.Caption;
                this.dt.Rows.Add(dataRow);
            }
        }
      
        private void btnAllSelected_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dt.Rows.Count; i++)
            {
                dt.Rows[i]["Selected"] = true;
            }
            this.gridView1.UpdateCurrentRow();
        }
        private void btnUnSelected_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dt.Rows.Count; i++)
            {
                dt.Rows[i]["Selected"] = false;
            }
            this.gridView1.UpdateCurrentRow();
        }
        private void btnResume_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dt.Rows.Count; i++)
            {
                dt.Rows[i]["Selected"] = true;
            }
            this.gridView1.UpdateCurrentRow();
            base.DialogResult = DialogResult.Abort;
            base.Close();
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            for (int num = 0; num < this.cols.Count; num++)
            {
                this.cols[num].VisibleIndex = (((bool)this.dt.Rows[num]["Selected"]) ? num : -1);
                this.cols[num].Caption = this.dt.Rows[num]["Caption"].ToString();
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Abort;
            base.Close();
        }
    }
}

后台页面
namespace Nbjjy.Utilities
{
    partial class CustomColumnDialog
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnAllSelected = new DevExpress.XtraEditors.SimpleButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnResume = new DevExpress.XtraEditors.SimpleButton();
            this.gridControl1 = new DevExpress.XtraGrid.GridControl();
            this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
            this.gridColumn1 = new DevExpress.XtraGrid.Columns.GridColumn();
            this.gridColumn2 = new DevExpress.XtraGrid.Columns.GridColumn();
            this.btnUnSelected = new DevExpress.XtraEditors.SimpleButton();
            this.btnOK = new DevExpress.XtraEditors.SimpleButton();
            this.btnCancel = new DevExpress.XtraEditors.SimpleButton();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
            this.SuspendLayout();
            //
            // btnAllSelected
            //
            this.btnAllSelected.Location = new System.Drawing.Point(318, 19);
            this.btnAllSelected.Name = "btnAllSelected";
            this.btnAllSelected.Size = new System.Drawing.Size(75, 23);
            this.btnAllSelected.TabIndex = 1;
            this.btnAllSelected.Text = "全选(&A)";
            this.btnAllSelected.Click += new System.EventHandler(this.btnAllSelected_Click);
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.btnResume);
            this.groupBox1.Controls.Add(this.gridControl1);
            this.groupBox1.Controls.Add(this.btnAllSelected);
            this.groupBox1.Controls.Add(this.btnUnSelected);
            this.groupBox1.Controls.Add(this.btnOK);
            this.groupBox1.Controls.Add(this.btnCancel);
            this.groupBox1.Location = new System.Drawing.Point(9, 6);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(406, 246);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            //
            // btnResume
            //
            this.btnResume.Location = new System.Drawing.Point(318, 67);
            this.btnResume.Name = "btnResume";
            this.btnResume.Size = new System.Drawing.Size(75, 23);
            this.btnResume.TabIndex = 3;
            this.btnResume.Text = "还原(&R)";
            this.btnResume.Click += new System.EventHandler(this.btnResume_Click);
            //
            // gridControl1
            //
            this.gridControl1.Location = new System.Drawing.Point(12, 19);
            this.gridControl1.MainView = this.gridView1;
            this.gridControl1.Name = "gridControl1";
            this.gridControl1.Size = new System.Drawing.Size(283, 215);
            this.gridControl1.TabIndex = 0;
            this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
            this.gridView1});
            //
            // gridView1
            //
            this.gridView1.ColumnPanelRowHeight = 20;
            this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
            this.gridColumn1,
            this.gridColumn2});
            this.gridView1.FixedLineWidth = 1;
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;
            this.gridView1.GridControl = this.gridControl1;
            this.gridView1.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Never;
            this.gridView1.Name = "gridView1";
            this.gridView1.OptionsCustomization.AllowFilter = false;
            this.gridView1.OptionsCustomization.AllowSort = false;
            this.gridView1.OptionsDetail.EnableMasterViewMode = false;
            this.gridView1.OptionsMenu.EnableColumnMenu = false;
            this.gridView1.OptionsMenu.EnableFooterMenu = false;
            this.gridView1.OptionsView.ColumnAutoWidth = false;
            this.gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
            this.gridView1.OptionsView.ShowGroupPanel = false;
            this.gridView1.OptionsView.ShowIndicator = false;
            this.gridView1.RowHeight = 20;
            //
            // gridColumn1
            //
            this.gridColumn1.AppearanceHeader.Options.UseTextOptions = true;
            this.gridColumn1.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            this.gridColumn1.Caption = "选择";
            this.gridColumn1.FieldName = "Selected";
            this.gridColumn1.Name = "gridColumn1";
            this.gridColumn1.OptionsColumn.AllowMove = false;
            this.gridColumn1.OptionsColumn.AllowSize = false;
            this.gridColumn1.Visible = true;
            this.gridColumn1.VisibleIndex = 0;
            this.gridColumn1.Width = 40;
            //
            // gridColumn2
            //
            this.gridColumn2.AppearanceHeader.Options.UseTextOptions = true;
            this.gridColumn2.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            this.gridColumn2.Caption = "列名";
            this.gridColumn2.FieldName = "Caption";
            this.gridColumn2.Name = "gridColumn2";
            this.gridColumn2.OptionsColumn.AllowMove = false;
            this.gridColumn2.OptionsColumn.AllowSize = false;
            this.gridColumn2.Visible = true;
            this.gridColumn2.VisibleIndex = 1;
            this.gridColumn2.Width = 220;
            //
            // btnUnSelected
            //
            this.btnUnSelected.Location = new System.Drawing.Point(318, 43);
            this.btnUnSelected.Name = "btnUnSelected";
            this.btnUnSelected.Size = new System.Drawing.Size(75, 23);
            this.btnUnSelected.TabIndex = 2;
            this.btnUnSelected.Text = "全清(&L)";
            this.btnUnSelected.Click += new System.EventHandler(this.btnUnSelected_Click);
            //
            // btnOK
            //
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOK.Location = new System.Drawing.Point(318, 187);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 23);
            this.btnOK.TabIndex = 4;
            this.btnOK.Text = "确定(&O)";
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            //
            // btnCancel
            //
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(318, 211);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 5;
            this.btnCancel.Text = "取消(&C)";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            //
            // CustomColumnDialog
            //
            this.AcceptButton = this.btnOK;
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(424, 258);
            this.ControlBox = false;
            this.Controls.Add(this.groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "CustomColumnDialog";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "自定义列";
            this.groupBox1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
            this.ResumeLayout(false);
        }
        #endregion
        private DevExpress.XtraEditors.SimpleButton btnAllSelected;
        private System.Windows.Forms.GroupBox groupBox1;
        private DevExpress.XtraEditors.SimpleButton btnResume;
        private DevExpress.XtraGrid.GridControl gridControl1;
        private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
        private DevExpress.XtraEditors.SimpleButton btnUnSelected;
        private DevExpress.XtraEditors.SimpleButton btnOK;
        private DevExpress.XtraEditors.SimpleButton btnCancel;
        private DevExpress.XtraGrid.Columns.GridColumn gridColumn1;
        private DevExpress.XtraGrid.Columns.GridColumn gridColumn2;
    }
}

其它项目调用方法如下:
new CustomColumnDialog(this.gridView1.Columns).ShowDialog();
回复

使用道具 举报

0

精华

940

贡献

167

赞扬

正版授权组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
237
软币
2692
在线时间
264 小时
注册时间
2013-6-17
发表于 2013-8-31 23:19:01 | 显示全部楼层
建议配上图,谢谢
回复

使用道具 举报

0

精华

100

贡献

33

赞扬

帖子
132
软币
3320
在线时间
53 小时
注册时间
2013-8-31
 楼主| 发表于 2013-9-1 14:59:07 | 显示全部楼层
linuxpeng 发表于 2013-8-31 23:19
建议配上图,谢谢

我上传了统一放在一编文章里面
有图有真相
http://www.dxper.net/thread-1118-1-1.html
(出处: DXPER开发者论坛)

评分

参与人数 1贡献 +1 赞扬 +1 收起 理由
羽叶 + 1 + 1 感谢分享

查看全部评分

回复

使用道具 举报

0

精华

5097

贡献

5311

赞扬

管理员

帖子
1154
软币
21035
在线时间
4325 小时
注册时间
2013-6-7

黄马甲

发表于 2013-9-1 16:38:43 | 显示全部楼层
建议图和文章放在一起
回复

使用道具 举报

0

精华

935

贡献

265

赞扬

帖子
4122
软币
9958
在线时间
366 小时
注册时间
2013-7-21
发表于 2013-9-1 18:34:33 | 显示全部楼层
感觉还不错,不过文章跟图不放在一起,有些人想收藏也不是很方便。个人建议呵呵
回复

使用道具 举报

0

精华

100

贡献

33

赞扬

帖子
132
软币
3320
在线时间
53 小时
注册时间
2013-8-31
 楼主| 发表于 2013-9-2 21:51:16 | 显示全部楼层
lhmlyx2723356 发表于 2013-9-1 18:34
感觉还不错,不过文章跟图不放在一起,有些人想收藏也不是很方便。个人建议呵呵 ...

图片放在这编文章了http://www.dxper.net/thread-1118-1-1.html
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-5-4 03:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表