[英]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.