DataGridView的复选框列的选中事件:DataGirdCheckBoxColumn 列 选中 事件

今天在winform下使用DataGridView时,使用了checkbox列。一般我们可以通过一个按钮的单击事件中,来判断该列是否被选中。但这样似乎不太人性化。因此改为当checkbox被选中时,执行任务。
可别小看了这个问题。我试了很多的datagridview事件,比如 cellClick CellContentChanged 等事件,要不就是无法扑捉,要不就是得到的值为选中前的值:false 。
当然,最后无意找到了正确的事件:cellContentClick 事件。在该事件中可得知选中前后的值,代码如下:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 3 && Convert.ToInt32(tbCpiName.Tag)>0 )//当单击复选框,同时处于组合编辑状态时
    {
        DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        bool ifcheck1 = Convert.ToBoolean(cell.FormattedValue);
        bool ifcheck2 = Convert.ToBoolean(cell.EditedFormattedValue);

        if (ifcheck1 != ifcheck2)
        {
            BCItem bcitem = new BCItem();
            bcitem.B_bciID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["bciID"].Value);
            bcitem.C_CpiID = Convert.ToInt32(tbCpiName.Tag);

            if (ifcheck2)//将该项目与组合关联
            {
                bllcp.AddCompageAndItem(bcitem);//关联
            }
            else
            {
                bllcp.DeleteCompageAndItem(bcitem);//取消关联
            }
        }
    }
}

http://www.cnblogs.com/lyamlf123/articles/1147292.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值