开发者论坛

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

[源码] DataGridView 自定义显示列控件制作方法

[复制链接]

0

精华

100

贡献

33

赞扬

帖子
132
软币
3320
在线时间
53 小时
注册时间
2013-8-31
发表于 2013-9-2 21:49:14 | 显示全部楼层 |阅读模式
什么也不用说了直接上代码:

后台代码
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.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnOK = new System.Windows.Forms.Button();
            this.btnUnSelected = new System.Windows.Forms.Button();
            this.btnAllSelected = new System.Windows.Forms.Button();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Column1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
            this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.btnCancel);
            this.groupBox1.Controls.Add(this.btnOK);
            this.groupBox1.Controls.Add(this.btnUnSelected);
            this.groupBox1.Controls.Add(this.btnAllSelected);
            this.groupBox1.Controls.Add(this.dataGridView1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(347, 280);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            //
            // btnCancel
            //
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(260, 213);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 33);
            this.btnCancel.TabIndex = 1;
            this.btnCancel.Text = "取  消";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            //
            // btnOK
            //
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOK.Location = new System.Drawing.Point(260, 174);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 33);
            this.btnOK.TabIndex = 1;
            this.btnOK.Text = "确  定";
            this.btnOK.UseVisualStyleBackColor = true;
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            //
            // btnUnSelected
            //
            this.btnUnSelected.Location = new System.Drawing.Point(260, 83);
            this.btnUnSelected.Name = "btnUnSelected";
            this.btnUnSelected.Size = new System.Drawing.Size(75, 33);
            this.btnUnSelected.TabIndex = 1;
            this.btnUnSelected.Text = "全  清";
            this.btnUnSelected.UseVisualStyleBackColor = true;
            this.btnUnSelected.Click += new System.EventHandler(this.btnUnSelected_Click);
            //
            // btnAllSelected
            //
            this.btnAllSelected.Location = new System.Drawing.Point(260, 44);
            this.btnAllSelected.Name = "btnAllSelected";
            this.btnAllSelected.Size = new System.Drawing.Size(75, 33);
            this.btnAllSelected.TabIndex = 1;
            this.btnAllSelected.Text = "全  选";
            this.btnAllSelected.UseVisualStyleBackColor = true;
            this.btnAllSelected.Click += new System.EventHandler(this.btnAllSelected_Click);
            //
            // dataGridView1
            //
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.BackgroundColor = System.Drawing.Color.PeachPuff;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2});
            this.dataGridView1.Location = new System.Drawing.Point(12, 20);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.Size = new System.Drawing.Size(237, 248);
            this.dataGridView1.TabIndex = 0;
            //
            // Column1
            //
            this.Column1.DataPropertyName = "Selected";
            this.Column1.HeaderText = "选择";
            this.Column1.Name = "Column1";
            this.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            this.Column1.Width = 70;
            //
            // Column2
            //
            this.Column2.DataPropertyName = "Caption";
            this.Column2.HeaderText = "列名";
            this.Column2.Name = "Column2";
            //
            // CustomColumnDialog
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(347, 280);
            this.Controls.Add(this.groupBox1);
            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.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Button btnOK;
        private System.Windows.Forms.Button btnUnSelected;
        private System.Windows.Forms.Button btnAllSelected;
        private System.Windows.Forms.DataGridViewCheckBoxColumn Column1;
        private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
    }
}
前台代码:
using System;
using System.Data;
using System.Windows.Forms;

namespace Nbjjy.Utilities
{
    public partial class CustomColumnDialog : Form
    {
        private DataTable dt;
        private DataGridViewColumnCollection cols;
        public CustomColumnDialog(DataGridViewColumnCollection cols)
        {
            InitializeComponent();
            this.cols = cols;
            this.GridColumn();
            this.dataGridView1.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 (DataGridViewColumn dataGridViewColumn in cols)
            {
                DataRow dataRow = this.dt.NewRow();
                dataRow["Selected"] = (dataGridViewColumn.Visible != false);
                dataRow["Caption"] = dataGridViewColumn.HeaderText;
                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["Selected"] = true;
            }
            this.dataGridView1.Update();
        }

        private void btnUnSelected_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dt.Rows.Count; i++)
            {
                dt.Rows["Selected"] = false;
            }
            this.dataGridView1.Update();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            for (int num = 0; num < this.cols.Count; num++)
            {
                this.cols[num].Visible = (((bool)this.dt.Rows[num]["Selected"]) ? Convert.ToBoolean(num) : false);
                this.cols[num].HeaderText = this.dt.Rows[num]["Caption"].ToString();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Abort;
            base.Close();
        }
    }
}

图文效果就像下面这编文章中的自定列效果是一样的
有图有真相
http://www.dxper.net/thread-1118-1-1.html
(出处: DXPER开发者论坛)


回复

使用道具 举报

0

精华

0

贡献

14

赞扬

帖子
27
软币
188
在线时间
11 小时
注册时间
2013-10-21
发表于 2013-10-22 00:15:49 | 显示全部楼层
好东西 学习了 最后能有源码下载就更好了
回复

使用道具 举报

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

GMT+8, 2024-5-13 22:50

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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