请选择 进入手机版 | 继续访问电脑版

无忧编程_ASP.NET  / C# / PHP 程序员的软件世界

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 942|回复: 0

为DataGridView列标题增加复选框checkbox

[复制链接]

374

主题

405

帖子

1676

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1676
发表于 2018-7-9 13:47:37 | 显示全部楼层 |阅读模式
为DataGridView列标题增加复选框checkbox
详细代码如下:
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataGridViewCheckBox
{
    public partial class Form1 : Form
    {
        // 创建一个DataGridView实例
        private DataGridView dgv = new DataGridView();
        // 创建一个CheckBox实例
        private CheckBox userEnabled = new CheckBox();

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // 绑定CheckBox发生勾选操作时的事件
            userEnabled.CheckedChanged += userEnabled_CheckedChanged;
            // 重绘dgv的CellPainting方法,在列标题出绘制复选框
            dgv.CellPainting += dgv_CellPainting;

            this.dgv.AllowUserToResizeRows = false;
            this.dgv.AllowUserToResizeColumns = true;
            this.dgv.Dock = DockStyle.Fill;

            // 添加三列
            this.dgv.Columns.Add("UserName", "UserName");
            // 添加复选框列
            this.dgv.Columns.Add(new DataGridViewCheckBoxColumn());
            this.dgv.Columns[1].HeaderText = "Enabled";
            this.dgv.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.dgv.Columns.Add("Age", "Age");

            // 插入演示数据
            for (int i = 1; i <= 3; i++)
            {
                this.dgv.Rows.Add("Name" + i.ToString(), 0, i.ToString());
            }


            this.userEnabled.Visible = false;
            this.userEnabled.Text = "";

            // 向Form1窗体增加这两个实例
            this.Controls.Add(dgv);
            this.Controls.Add(userEnabled);
        }

        private void userEnabled_CheckedChanged(object send, System.EventArgs e)
        {
            for (int i = 0; i <= this.dgv.RowCount - 1; i++)
            {
                this.dgv.Rows[i].Cells[1].Value = this.userEnabled.Checked ? 1 : 0;
            }
        }

        private void dgv_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex == 1)
            {
                // 在指定列显示userEnabled实例
                Point p = this.dgv.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
                p.Offset(this.dgv.Left +5, this.dgv.Top + 5);
                this.userEnabled.Location = p;
                this.userEnabled.Size = new Size(15, 15);
                this.userEnabled.Visible = true;
                this.userEnabled.BringToFront();
            }
        }
    }
}

windows .net(C#+MSSQL) linux(php+mysql)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|软件编程 ( 鄂ICP备11006601号鄂公网安备 42011102001337号 |

GMT+8, 2018-11-13 01:34 , Processed in 0.166342 second(s), 24 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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