简体   繁体   English

DataGridViewCheckBoxColumn问题开始查看复选框是否已选中

[英]DataGridViewCheckBoxColumn issues getting to see if the checkbox selected

So this one sounds simple, but for the life of me I cannot see why this is not working (Windows Forms). 因此,这听起来很简单,但是对于我来说,我一生都看不到为什么它不起作用(Windows Forms)。 Any help much appreciated. 任何帮助,不胜感激。

I have a DataGridViewCheckBoxColumn set as per following; 我有一个DataGridViewCheckBoxColumn设置如下:

DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
col.DataPropertyName = "Data";
col.Name = "DataName";
col.HeaderText = "Data Header";
DataGridView.Columns.Add(col);

This works and my grid allows users to change check/uncheck the checkbox. 这可行,我的网格允许用户更改选中/取消选中复选框。 Great now when they press save, to save it to the db i have the following: 现在很好,当他们按保存时,要将其保存到数据库,我有以下内容:

foreach (DataGridViewRow row in dgUsers.Rows)
{
 bool bData = (bool)row.Cells["Data"].Value;
}

To read teh checkbox, I have also tried .selected and also tried to view the checkbox cell. 要阅读复选框,我还尝试了.selected并尝试查看复选框单元格。 in the following ways: 通过以下方式:

DataGridViewCheckBoxCell cell = row.Cells["Data"] as DataGridViewCheckBoxCell;
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["Data"];

However none of this works. 但是,这些都不起作用。 I just want to get the boolean of if it true or false. 我只想获取布尔值是对还是错。

Any ideas. 有任何想法吗。

Thanks 谢谢

So after extensive research I think I may have found an answer. 因此,经过大量研究,我想我可能已经找到了答案。 I have a datagridview with numerous data one of which is a checkbox cell so users can mark/unmark the data. 我有一个包含大量数据的datagridview,其中一个是复选框单元格,因此用户可以标记/取消标记数据。 Now because I have a button which will loop through the datagridview rows and save the changes to a database. 现在,因为我有一个按钮,它将循环遍历datagridview行并将更改保存到数据库。 There is a possibility that the checkbox has not fully committed the value change. 复选框可能没有完全提交值更改。 Therefore we can use the edited value as follows: 因此,我们可以按如下方式使用编辑后的值:

DataGridViewCheckBoxCell.EditedFormattedValue

Gets the current, formatted value of the cell, regardless of whether the cell is in edit mode and the value has not been committed 获取单元格的当前格式值,而不管该单元格是否处于编辑模式且该值尚未提交

. So for my example it would be the following: 因此,对于我的示例,它将是以下内容:

DataGridViewCheckBoxCell cell = row.Cells["Data"] as DataGridViewCheckBoxCell;
bool bData = (bool)cell.EditedFormattedValue 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么未选中DatagridviewCheckboxColumn的复选框? - Why isn't DatagridviewCheckboxColumn's checkbox getting checked? 选中第一个checkBox列时获取两个DataGridViewCheckBoxColumn - Getting two DataGridViewCheckBoxColumn when first checkBox column is checked DataGridViewCheckBoxColumn-如果复选框已选中 - DataGridViewCheckBoxColumn - If checkBox checked issue 如何在DatagridviewCheckBoxColumn的Header添加复选框控件? - How to add checkbox control at the Header of DatagridviewCheckBoxColumn? 是否可以在DataGridViewCheckBoxColumn中获取所选项目的索引? - Is it possible to get index of selected items in DataGridViewCheckBoxColumn? 在复选框列表中获取选定的值 - Getting the selected values in a checkbox list Datagridview使用以下方法在某些单元格上添加复选框 <DataGridViewCheckBoxColumn> ,而不是专栏 - Datagridview add Checkbox on some cell using <DataGridViewCheckBoxColumn>, not a column 在GridTemplateColumn中使用DataKeyNames和CheckBox来查看已选择的行? - Use DataKeyNames and CheckBox in GridTemplateColumn to see what rows have been selected? .net datagridviewcheckboxcolumn - .net datagridviewcheckboxcolumn ASP.NET Webform:获取代码隐藏上选定复选框的值 - ASP.NET Webform: Getting values of selected checkbox on codebehind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM