效果图是这样的,如何把CheckBox放到左上角是最重要的。

添加

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 CheckBoxClickedHandler OnCheckBoxClicked; public DatagridViewCheckBoxHeaderCell()
{
} 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 / ) - (s.Width / );
p.Y = cellBounds.Location.Y +
(cellBounds.Height / ) - (s.Height / );
_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);
} 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;
if (OnCheckBoxClicked != null)
{
OnCheckBoxClicked(_checked);
this.DataGridView.InvalidateCell(this);
} }
base.OnMouseClick(e);
}
}

添加方法 InitColumnInfo() 方法,代码如下。

 private void InitColumnInfo()
{
int index = ; DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
colCB.HeaderCell = cbHeader;
colCB.HeaderText = "";
colCB.FillWeight = ;
cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
dataGridView2.Columns.Insert(index, colCB); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Blue;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Blue;
dataGridView2.Columns[index].DefaultCellStyle = dataGridViewCellStyle2;
}

在Clicked事件中添加代码

private void cbHeader_OnCheckBoxClicked(bool state)
{
//这一句很重要结束编辑状态
dataGridView2.EndEdit();
dataGridView2.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[].Value = state);
}

最后在窗体的Load事件中,调用 InitColumnInfo() 方法就可以了。

啥?客户叫在DataGridView的左上角添加CheckBox?的更多相关文章

  1. winform 中 给DataGridView的表头添加CheckBox

    在C/S架构中,给DataGridView的表头添加CheckBox控件: 添加类:   /// <summary>       /// 给DataGridView添加全选       / ...

  2. DataGridView 中添加CheckBox和常用处理方式 .

    DataGridView 中添加CheckBox和常用处理方式 文章1 转载:http://blog.csdn.net/pinkey1987/article/details/5267934 DataG ...

  3. DataGridView中添加CheckBox列用于选择行

    DataGridView中添加CheckBox列用于选择行 1,编辑DataGridView,添加一列 CheckBox ,Name 赋值为 "select",如下图: 2,取消 ...

  4. 设置DataGridView 显示自己添加编辑的列名,不动态显示数据库本身

    设置DataGridView 显示自己添加编辑的列名,不动态显示数据库本身. 方法: (1)界面操作,把DataGridView控件拖放在窗体中,就看到DataGridView控件的右上角有个小三角, ...

  5. Winform开发 如何为dataGridView 添加CheckBox列,并获取选中行

    //添加CheckBox列 DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn(); columncb.Heade ...

  6. Jquery动态在td中添加checkbox

    如图:想要在这个id为headId的<td>中,用jquery动态添加checkbox 代码如下 : data是我用ajax 从后台获取的数据,里面含有若干个user类,我想把所有的人名字 ...

  7. 给C#的treeview控件的部分节点添加checkbox

    一.先初始化treeview this.treeView1.CheckBoxes = true; this.treeView1.ShowLines = false; this.treeView1.Dr ...

  8. 实现DataGridView控件中CheckBox列的使用

    最近做WindowsForms程序,使用DataGridView控件时,加了一列做选择用,发现CheckBox不能选中.搜索后,要实现DataGridView的CellContentClick事件,将 ...

  9. wpf中为DataGrid添加checkbox支持多选全选

    项目中用到DataGrid, 需要在第一列添加checkbox, 可以多选.全选. 其中涉及的概念DataTemplate, DataGridCellStyle, DataGridCellContro ...

随机推荐

  1. 4sumii

    problem description: there is four number list named A,B,C,D; now you should out put the num of  tup ...

  2. 分享一下在aspx页面弹框的设置代码

    public static class MessageBox { /// <summary> /// 显示消息提示对话框 /// </summary> /// <para ...

  3. 解决iframe在移动端(主要iPhone)上的问题

    前言 才发现已经有一段时间没有写博客了,就简单的说了最近干了啥吧.前段时间忙了杂七杂八的事情,首先弄了个个人的小程序,对的,老早就写了篇从零入手微信小程序开发,然后到前段时间才弄了个简单的个人小程序, ...

  4. 异步任务spring @Async注解源码解析

    1.引子 开启异步任务使用方法: 1).方法上加@Async注解 2).启动类或者配置类上@EnableAsync 2.源码解析 虽然spring5已经出来了,但是我们还是使用的spring4,本文就 ...

  5. java中new关键字和newInstance()方法的区别

    1> new是一个关键字,可以说是一个指令: newInstance()是一个方法,Class对象的一个方法. 2> new主要作用是在内存中生成一个实例,而这个类可以没有提前加载到内从中 ...

  6. Avio红外热像仪在汽车行业的应用

    红外热像仪利用红外探测器接受被测目标的红外辐射能量,将红外辐射能量转换成带有温度信息的图像信号,并通过显视屏等显示工具显示红外热图像.这种红外热图像与物体表面的温度分布相对应.红外热像仪能够将探测到的 ...

  7. .NET Core使用微软官方类库实现汉字转拼音

    一.NuGet包 拼音:Install-Package SimplifiedChinesePinYinConversion 简体-繁体互转:Install-Package TraditionalChi ...

  8. ubuntukylin18.04Lts和deepin15.5与win10 1803双系统安装

    我首先安装的是ubuntu kylin(中文名优麒麟) 1.计算机右键选择管理磁盘,压缩卷设置空闲空间(第7步分区用) 2.重启时fn+f1进入bios设置界面. 3.关闭安全模式和快速启动,将boo ...

  9. Prefer ThreadLocalRandom over Random

    Java 7 has introduced a new random number generator - ThreadLocalRandom Normally to generate Random ...

  10. 两种方法轻松搞定-- Eclipse 安装FindBugs插件

    1安装:首先到官方网站下载最新版本FindBugs    http://findbugs.sourceforge.net/downloads.html              将 edu.umd.c ...