Winform 中DataGridView的checkbox列,当修改checkbox状态时实时获得其状态值
不知道大家有没有这样的经验,当点击或者取消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
Winform 中DataGridView的checkbox列,当修改checkbox状态时实时获得其状态值的更多相关文章
- C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法
下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...
- 【接上一篇】winform中dataGridView高度和宽度自适应填充完数据的高度和宽度,即dataGridView根据数据自适应大小
上一篇:winform中dataGridView高度自适应填充完数据的高度 winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度 ...
- winform中dataGridView高度自适应填充完数据的高度
// winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...
- WinForm中DataGridView复制选中单元格内容解决方案
WinForm中DataGridView鼠标选中单元格内容复制方案 1.CTR+C快捷键复制 前提:该控件ClipboardCopyMode属性设置值非Disable: 2.鼠标框选,自定义代码实现复 ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色
winform中dataGridView隔行显示不同的背景色,鼠标移动上显示不同颜色,离开后变回原色 先设置奇数行颜色,这个有个自带的属性AlternatingRowsDefaultCellStyle ...
- C# Winform中DataGridView的DataGridViewCheckBoxColumn CheckBox选中判断
1.DataGridViewCheckBoxColumn CheckBox是否选中 在判断DataGridView中CheckBox选中列的时候,用DataGridViewRow.Cells[0].F ...
- winform :DataGridView添加一列checkbox
#region 添加checkbox列 public void AddCheckBox() { DataGridViewCheckBoxColumn columncb = new D ...
- winform中DataGridView实现分页功能
WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载) 转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...
随机推荐
- cordova + ionic 使用中碰到的一些问题
cordova + ionic 使用中碰到的一些问题 No Content-Security-Policy meta tag found. Please add one when using ...
- workplace background
class:SysSetupFormRun public void run() { super(); this.design().colorScheme(FormColorScheme::RGB); ...
- linux出现user account has expired解决方案
SUSE Linux 用户user1登陆不了,确认密码没错,使用root用户登陆,su - user1 提示密码不对,passwd user1提示帐户过期user account hasexpired ...
- iOS项目中安装和使用 Cocoapods
1.首先我们要打开我们的终端: 2.在终端输入 这条命令 gem sources -l 2.1如果是和我是一样的显示,则镜像已添加,无需更改,如果不一样,则需要进行更改 这里输出的如果是 https ...
- 仿QQ消息气泡提醒
https://github.com/dkmeteor/Bubble-Notification 感谢这位作者,本例子只是封装了一下源码. 这是jar下载地址 下载之后只要引用就好.下面是一个最简单 ...
- LR12.53—第4课:准备Vuser脚本进行负载测试
第4课:准备Vuser脚本进行负载测试 在前面的课程中,您确认您的Vuser脚本的回放产生了真正的用户的精确仿真.下一个步骤是准备的脚本负载测试.如何将多用户系统同时工作的工作?会拖慢系统到不可接受的 ...
- border在IE6设置transparent无效
在ie6下给border设置transparent是无效的,解决办法如下: _border-color:tomato; /*For IE6-*/ _filter:chroma(color=tomato ...
- FlASK中的endpoint问题
先贴一点有关的flask代码,时间有限,我慢慢扩充 以下是flask源码中app.py中add_url_rule的代码. 主要是view_func -- endpoint -- url 之间的对应关 ...
- 采用UDP协议实现PIC18F97J60 ethernet bootloader
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). TCP/IP Stac ...
- UIViewController生命周期
UIViewController生命周期