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

   主要用到了DataGridView的CurrentCellDirtyStateChanged和CellValueChanged两个事件

   CurrentCellDirtyStateChanged事件是提交对checkbox状态的修改

     CellValueChanged事件是当状态提交后,也就是单元格值改变后做一些其它的操作

    (1). CurrentCellDirtyStateChanged事件代码:

void PositionListDataView_CurrentCellDirtyStateChanged( object sender, EventArgs e)
{
DataGridView grid = sender as DataGridView;
if (grid != null )
{
grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}

  (2). CellValueChanged事件代码:

void PositionListDataView_CellValueChanged( object sender, DataGridViewCellEventArgs e)
{
DataGridView grid = sender as DataGridView;
if (grid != null && e.RowIndex >= 0 )
{
if (grid.Columns[e.ColumnIndex].Name == " Check " )
{
DataTable dt = grid.DataSource as DataTable;
int pstnID = Convert.ToInt32(dt.Rows[e.RowIndex][ 1 ]); DataGridViewCheckBoxCell checkbox = grid.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell; // 获得checkbox列单元格
int result = 0 ;
if (checkbox != null && checkbox.Value.ToString() == " 1 " )
{
result = cuttingReport.UpdateR_RptRstnStandardAndBlendByCheck( this .reportID,pstnID, 1 , 0 );
}
else
{
result = cuttingReport.UpdateR_RptRstnStandardAndBlendByCheck( this .reportID, pstnID, 0 , 0 );
}
if (result < 1 )
{
MessageBox.Show( " 修改失败" , " 提示" , MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} }

另外:grid.Columns[e.ColumnIndex].Name == "Check" 中Name的值,是在生成DataGridView的Columns时添加的:

new DataGridViewCheckBoxColumn() { HeaderText = "Check", DataPropertyName = "Checked", Visible = true, Width = 45, Frozen = true, Name = "Check", TrueValue = 1, FalseValue = 0, IndeterminateValue = 0 }

  原文参考:http://www.cnblogs.com/gossip/archive/2008/12/02/1346047.html

最新文章

  1. SerialPort 串口开发
  2. waf2控件名
  3. Python学习推荐
  4. 如何在 iOS 8 中使用 Swift 实现本地通知(下)
  5. HDOJ2017字符串统计
  6. Python 处理server返回gzip内容
  7. Ubuntu 如何使用apt命令安装、升级、卸载软件
  8. 46. Lotus Notes中编程发送邮件(一)
  9. hdu3873 有约束条件的最短路
  10. Orace内置函数大全[转:http://www.cnblogs.com/lfx0692/articles/2395950.html]
  11. 初窥c++11:lambda函数及其用法
  12. 发布 Google Chrome插件教程
  13. layui之确认框
  14. LODOP设置打印机不存在不打印
  15. 洛谷 P1879 玉米田(状压DP入门题)
  16. css3图片旋转
  17. jenkins的svn路径中文问题
  18. linux常用命令:chgrp 命令
  19. cmake 手册详解
  20. Sublime Text 3 个人使用总结

热门文章

  1. 图形学基础教程02--顶点数据和SHADER寄存器 (带演示程序)
  2. django 项目的文件说明
  3. javaEE中关于dao层和services层的理解
  4. SQL Server提高事务复制效率优化(三)订阅初始化优化
  5. 多行SQL语句拼成一条数据
  6. 在线c++编译器(gcc)
  7. Python自动化 【第十二篇】:Python进阶-MySQL和ORM
  8. My97DatePicker 日期控制,开始时间不能&gt;结束时间,结束时间不能&lt;开始时间
  9. Md5 签名算法
  10. 用sass写栅格系统