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

posted @ 2009-07-21 19:57  emanlee  阅读(7143)  评论(1编辑  收藏  举报