您现在的位置是:网站首页> 编程资料编程资料
DataGridView - DataGridViewCheckBoxCell的使用介绍_实用技巧_
2023-05-25
260人已围观
简介 DataGridView - DataGridViewCheckBoxCell的使用介绍_实用技巧_
Datagridview是.net中最复杂的控件,由于人们对表格的格式要求多种多样,所以编写一个通用的Datagridview(类似JSF中的datatable)非常困难的。
Datagridview中,用户可以对行、列、单元格进行编程。如行中可以插入下拉列表、复选框、编辑框、单选框等多种控件。每种控件都以DataGridView开头。如:单选框类为DataGridViewCheckBoxCell。
DataGridViewCheckBoxCell有一些恶心的属性折磨了我很久,下加以详细说明。
FormattedValue属性:
可能大家已经习惯了用checked=true或者checked=false这样直观的语句来取得checkbox的值,但DataGridViewCheckBoxCell没有checked属性,而使用了更复杂的FormattedValue。
EditedFormattedValue属性:
当前checkbox的状态,不管它是不是已经是一个“确认值”。在我们在印象里,checkbox只有true或false。什么叫“确认值”呢?确认值是指:不管用户是不是已经离开该单元格(即确认该单元格最终的状态),都返回checkbox目前的值。乍一听,更糊涂了。举个例子加以解释:
(1) 初始时checkbox未选中,用户点了一下,于是checkbox会呈现勾选状态
这时,EditedFormattedValue=true,但FormattedValue=false,这是因为,用户没有“确认”这个值,这个checkbox仍然处于编辑状态;
(2) 初始时checkbox选中,用户点了一下,于是checkbox会呈现未勾选状态,然后用户点击其它单元格
这时,EditedFormattedValue=false,但FormattedValue=false,这是因为,用户离开这个单元格意味着用户已经“确认”这个值,这个checkbox不再处于编辑状态,它的EditedFormattedValue==FormattedValue
这时,EditedFormattedValue=false,但FormattedValue=false,这是因为,用户离开这个单元格意味着用户已经“确认”这个值,这个checkbox不再处于编辑状态,它的EditedFormattedValue==FormattedValue
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewCheckBoxCell chkBoxCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[Column_Id.Index];
if (chkBoxCell != null && ((bool)chkBoxCell.EditingCellFormattedValue == true || (bool)chkBoxCell.FormattedValue == true))
{
}
}
Datagridview中,用户可以对行、列、单元格进行编程。如行中可以插入下拉列表、复选框、编辑框、单选框等多种控件。每种控件都以DataGridView开头。如:单选框类为DataGridViewCheckBoxCell。
DataGridViewCheckBoxCell有一些恶心的属性折磨了我很久,下加以详细说明。
FormattedValue属性:
可能大家已经习惯了用checked=true或者checked=false这样直观的语句来取得checkbox的值,但DataGridViewCheckBoxCell没有checked属性,而使用了更复杂的FormattedValue。
EditedFormattedValue属性:
当前checkbox的状态,不管它是不是已经是一个“确认值”。在我们在印象里,checkbox只有true或false。什么叫“确认值”呢?确认值是指:不管用户是不是已经离开该单元格(即确认该单元格最终的状态),都返回checkbox目前的值。乍一听,更糊涂了。举个例子加以解释:
(1) 初始时checkbox未选中,用户点了一下,于是checkbox会呈现勾选状态
这时,EditedFormattedValue=true,但FormattedValue=false,这是因为,用户没有“确认”这个值,这个checkbox仍然处于编辑状态;
(2) 初始时checkbox选中,用户点了一下,于是checkbox会呈现未勾选状态,然后用户点击其它单元格
这时,EditedFormattedValue=false,但FormattedValue=false,这是因为,用户离开这个单元格意味着用户已经“确认”这个值,这个checkbox不再处于编辑状态,它的EditedFormattedValue==FormattedValue
这时,EditedFormattedValue=false,但FormattedValue=false,这是因为,用户离开这个单元格意味着用户已经“确认”这个值,这个checkbox不再处于编辑状态,它的EditedFormattedValue==FormattedValue
复制代码 代码如下:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewCheckBoxCell chkBoxCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[Column_Id.Index];
if (chkBoxCell != null && ((bool)chkBoxCell.EditingCellFormattedValue == true || (bool)chkBoxCell.FormattedValue == true))
{
}
}
您可能感兴趣的文章:
相关内容
- 三层+存储过程实现分页示例代码_实用技巧_
- ASP.NET中操作SQL数据库(连接字符串的配置及获取)_实用技巧_
- asp.net页面传值测试实例代码(前后台)_实用技巧_
- asp.net后台cs中的JSON格式变量在前台Js中调用方法(前后台示例代码)_实用技巧_
- 设置ASP.NET页面不被缓存(客户端/服务器端取消缓存方法)_实用技巧_
- 读取纯真IP数据库的公用组件接口QQWry.NET_实用技巧_
- c#实现根据网络IP显示地理位置功能示例_实用技巧_
- 关于中gridview 字符串截取的方法_实用技巧_
- aspxgridview CustomButtonCallback 不支持弹出消息提示解决方法_实用技巧_
- 页面爬虫(获取其他页面HTML)加载到自己页面示例_实用技巧_
