下面演示如何在 DataGridView 中动态绑定 CheckBox:

public class Test
{
/// <summary>
/// 构造器
/// </summary>
public Test()
{
InitializeComponent();
//生成全选checkbox
GenerateCheckbox();
}
/// <summary>
/// 全选按钮的选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void checkboxAll_CheckedChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
dr.Cells[].Value = ((CheckBox)dataGridView1.Controls.Find("checkboxAll", true)[]).Checked;
}
} /// <summary>
/// 生成全选Checkbox
/// </summary>
private void GenerateCheckbox()
{
//检查 dataGridView1 中是否至少绑定了一列
if (string.IsNullOrEmpty(dataGridView1.Columns[].Name))
return; //DataGridView 中专门用来显示 Checkbox 控件的列对象;dataGridView1 中添加此对象后,绑定数据时就会自动为每行创建一个新的 CheckBox 控件
//当然这一步也可以省略,换成直接在 dataGridView1 的 Columns 属性中添加一列,然后将其 ColumnType 设置成 DataGridViewCheckBoxColumn 即可
DataGridViewCheckBoxColumn gridViewCheckBoxCol = new DataGridViewCheckBoxColumn
{
Width = ,
HeaderText = "",
DefaultCellStyle =
{
Alignment = DataGridViewContentAlignment.MiddleCenter
},
ReadOnly = false //若为 true 则无法选择 CheckBox
}; //把 dataGridView1 的第一列设置成 CheckBox 格式的列
dataGridView1.Columns.Insert(, gridViewCheckBoxCol); //获取 dataGridView1 中表示单元格显示区域的矩形,通过矩形的位置来设置 CheckBox 的相对位置
Rectangle rectangle = dataGridView1.GetCellDisplayRectangle(, -, true);
rectangle.X = rectangle.Location.X + rectangle.Width + ;
rectangle.Y = rectangle.Location.Y + rectangle.Height + ; //新建一个用于“全选”的 CheckBox 对象 checkboxAll
CheckBox chkOfAll = new CheckBox();
chkOfAll.Name = "checkboxAll";
chkOfAll.Size = new Size(, );
chkOfAll.Location = rectangle.Location; //为 checkboxAll 绑定全选事件
chkOfAll.CheckedChanged += new EventHandler(checkboxAll_CheckedChanged); //把 checkboxAll 添加到 dataGridView1 中
dataGridView1.Controls.Add(chkOfAll);
}
}

DataGridView 动态绑定 CheckBox的更多相关文章

  1. Winform 中DataGridView的checkbox列,当修改checkbox状态时实时获得其状态值

    不知道大家有没有这样的经验,当点击或者取消datagridview的checkbox列时,比较难获得其状态是选中还是未选中,进而不好进行其它操作,下面就列出它的解决办法: 主要用到了DataGridV ...

  2. Datagridview 添加checkbox列,并判断Datagridview 中的checkbox列是否被选中

    Solution1://In Fill DataGridViewEvent : DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxC ...

  3. C# winform单元格的formatted值的类型错误 DataGridView中CheckBox列运行时候System.FormatException异常

    在DataGridView手动添加了CheckBox列;在窗体Show的时候,遇到一个错误:错误如下: DataGridView中发生一下异常:System.FormatException:单元格的F ...

  4. 关于datagridview中checkbox列在选中行的情况下无法操作值

    这几天做项目的时候碰到了个小问题,在datagridview中实现对checkbox列的全选和反选功能.代码如下              //全选              if (dataGrid ...

  5. DataGridView里CheckBox实现全选控制

    1. checkbox点击事件 private void myStyleDataGridView1_CellClick(object sender, DataGridViewCellEventArgs ...

  6. DataGridView 中CheckBox 获取状态

    /// <summary> /// /// </summary> /// <param name="sender"></param> ...

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

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

  8. DataGridView 使用CheckBox选中行

    在winform中使用checbox很多.上次那个项目里就用到了,写了一个不太好用,后来翻阅了一下微软提供的样码,我觉得有必要给大家分享一下. // This event handler manual ...

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

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

随机推荐

  1. codevs1792 分解质因数

    题目描述 Description 编写一个把整数N分解为质因数乘积的程序. 输入描述 Input Description 输入一个整数 N 输出描述 Output Description 输出 分解质 ...

  2. 浅识MySQL

    MySQL常用语句 #操作数据库 ##创建数据库 CREATE DATABASE `dbName`; ##切换数据库 USE `dbName`; ##查看所有数据库 SHOW DATABASES; # ...

  3. linux程序命令行选项的3种风格:unix、gnu、x toolkit

    In Unix-like systems, the ASCII hyphen-minus is commonly used to specify options. The character is u ...

  4. ImportError: No module named MySQLdb解决办法

    http://blog.slogra.com/post-429.html http://blog.sina.com.cn/s/blog_74a7e56e0101a7qy.html 今天突发奇想在服务器 ...

  5. 002 static and default route

    r2(config)#ip route 192.168.1.0 255.255.255.0 192.168.2.1 r1(config)#ip route 192.168.3.0 255.255.25 ...

  6. swift学习笔记(四)关于类的继承

    在swift中,继承是区分类与其它对象的基本特征 继承后的子类能够重写父类的方法,包含类方法和实例方法,属性和附属脚本(subscript) 在继承过程中,构造器方法init()是不被继承的,须要显示 ...

  7. MapReduce的Reduce side Join

    1. 简单介绍 reduce side  join是全部join中用时最长的一种join,可是这样的方法可以适用内连接.left外连接.right外连接.full外连接和反连接等全部的join方式.r ...

  8. java Bean及其使用

    1 getter/setter方法 java例子: public class student { private String name; public String getName() { retu ...

  9. 编程细节 —— 按值传递、按引用传递(final、const)

    System.out,out 是 System 类内定义的静态 final PrinterStream 变量: public final class System { ... public final ...

  10. BZOJ_3175_[Tjoi2013]攻击装置_二分图匹配

    BZOJ_3175_[Tjoi2013]攻击装置_二分图匹配Description 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置 ...