博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DataGridView列头checkbox 分类: .NET ...
阅读量:4546 次
发布时间:2019-06-08

本文共 5365 字,大约阅读时间需要 17 分钟。

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;using System.Data.SqlClient;namespace WinformOperateDataGridView{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void 查询_Click(object sender, EventArgs e)        {            DataTable dtTemp = new DataTable();             using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123456;database=test;"))            {                SqlDataAdapter sa = new SqlDataAdapter("select * from test", con);                sa.Fill(dtTemp);            }            this.dataGridView1.DataSource = dtTemp;            //创建复选框列               DataGridViewCheckBoxColumn checkboxCol = new DataGridViewCheckBoxColumn();            //创建复选框列头单元格            datagridviewCheckboxHeaderCell ch = new datagridviewCheckboxHeaderCell();            //设置复选框那列的单元格为复选框            checkboxCol.HeaderCell = ch;                        //将此复选框列添加到DataGridView中            this.dataGridView1.Columns.Add(checkboxCol);            ch.OnCheckBoxClicked += new datagridviewcheckboxHeaderEventHander(ch_OnCheckBoxClicked);//关联单击事件        }        private void ch_OnCheckBoxClicked(object sender, datagridviewCheckboxHeaderEventArgs e)        {             foreach (DataGridViewRow dgvRow in this.dataGridView1.Rows)             {                 if (e.CheckedState)                 {                     dgvRow.Cells[2].Value = true;                 }                 else                 {                     dgvRow.Cells[2].Value = false;                 }             }         }    }    //定义触发单击事件的委托    delegate void datagridviewcheckboxHeaderEventHander(object sender, datagridviewCheckboxHeaderEventArgs e);    class datagridviewCheckboxHeaderEventArgs : EventArgs    {        private bool checkedState = false;         public bool CheckedState        {            get { return checkedState; }            set { checkedState = value; }        }     }    class datagridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell     {         Point checkBoxLocation;         Size checkBoxSize;         bool _checked = false;         Point _cellLocation = new Point();         System.Windows.Forms.VisualStyles.CheckBoxState _cbState =             System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;         public event datagridviewcheckboxHeaderEventHander OnCheckBoxClicked;           //绘制列头checkbox         protected override void Paint(System.Drawing.Graphics graphics,            System.Drawing.Rectangle clipBounds,            System.Drawing.Rectangle cellBounds,            int rowIndex,            DataGridViewElementStates dataGridViewElementState,            object value,            object formattedValue,            string errorText,            DataGridViewCellStyle cellStyle,            DataGridViewAdvancedBorderStyle advancedBorderStyle,            DataGridViewPaintParts paintParts)         {             base.Paint(graphics, clipBounds, cellBounds, rowIndex,                 dataGridViewElementState, value,                 formattedValue, errorText, cellStyle,                 advancedBorderStyle, paintParts);             Point p = new Point();             Size s = CheckBoxRenderer.GetGlyphSize(graphics,             System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);             p.X = cellBounds.Location.X +                 (cellBounds.Width / 2) - (s.Width / 2) - 1;//列头checkbox的X坐标             p.Y = cellBounds.Location.Y +                 (cellBounds.Height / 2) - (s.Height / 2);//列头checkbox的Y坐标             _cellLocation = cellBounds.Location;             checkBoxLocation = p;             checkBoxSize = s;             if (_checked)                 _cbState = System.Windows.Forms.VisualStyles.                     CheckBoxState.CheckedNormal;             else                 _cbState = System.Windows.Forms.VisualStyles.                     CheckBoxState.UncheckedNormal;             CheckBoxRenderer.DrawCheckBox             (graphics, checkBoxLocation, _cbState);         }            ///          /// 点击列头checkbox单击事件         ///          protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)         {              Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);             if (p.X >= checkBoxLocation.X && p.X <=                 checkBoxLocation.X + checkBoxSize.Width             && p.Y >= checkBoxLocation.Y && p.Y <=                 checkBoxLocation.Y + checkBoxSize.Height)             {                 _checked = !_checked;                   //获取列头checkbox的选择状态                 datagridviewCheckboxHeaderEventArgs ex = new datagridviewCheckboxHeaderEventArgs();                 ex.CheckedState = _checked;                  object sender = new object();//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例                  if (OnCheckBoxClicked != null)                 {                     OnCheckBoxClicked(sender, ex);//触发单击事件                     this.DataGridView.InvalidateCell(this);                 }              }             base.OnMouseClick(e);         }      }    }

PS:有的网友在MS SQL数据库里添加bit类型的字段,这个是没有必要的。

参考地址:

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/configman/archive/2013/03/13/4657567.html

你可能感兴趣的文章
CSS div 高度满屏
查看>>
页面回发速度由 6 秒减少为 0.6 秒的真实案例!
查看>>
一种实现C++反射功能的想法(一)
查看>>
lvs+keepalived+nginx高性能负载均衡集群
查看>>
XXL-Job高可用集群搭建
查看>>
JDBC
查看>>
CodeForces - 123E Maze
查看>>
ZOJ 1709 Oil Deposits(dfs,连通块个数)
查看>>
安卓开源项目周报0308
查看>>
记可敬可佩的老车同志
查看>>
Maven in 5 Minutes(Windows)
查看>>
常用前端开发工具合集
查看>>
T-SQL:SQL Server-数据开发(经典)
查看>>
IOS 截取字符串
查看>>
键盘控制div移动并且解决停顿问题(原生js)
查看>>
矩阵快速幂优化线性递推
查看>>
基础网络流学习笔记
查看>>
Linux文件夹文件创建、删除
查看>>
归并排序 稳定
查看>>
xml约束技术之dtd
查看>>