【发布时间】:2020-04-18 11:51:56
【问题描述】:
我已经查看了有关此的其他帖子,但除了空字符串之外,我似乎无法从 DataGridViewCheckBoxCell 中获取值。
这是我在实例化新行时尝试设置单元格的真值和假值但无济于事。顺便说一句,我在运行时创建所有列和行,所以编辑器不适用。
//Default checkBox true and false values are the same and must be set to true and false
if (Program.mainForm.TableMainGridView.Columns[column.Name] is DataGridViewCheckBoxColumn)
{
val = false;
DataGridViewCheckBoxCell cell = new DataGridViewCheckBoxCell();
cell.FalseValue = false;
cell.TrueValue = true;
Program.mainForm.TableMainGridView.Rows[index].Cells[column.Name] = cell;
}
然后我尝试了this question 的解决方案,方法是将单元格转换为DataGridViewCheckBoxCell,然后在CellContentClick 事件上检索它的值(无论我是否尝试设置cell.FalseValue 和cell.TrueValue),并且无济于事。
DataGridViewCheckBoxCell cell = Program.mainForm.TableMainGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(cell.Value) == true)
{
value = true;
}
else
{
value = false;
}
我还看到有一种方法可以设置列本身的 true 和 false 值,如果它确实设置了所有后续单元格的值,那将是理想的,但似乎也没有任何作用。
是否有其他人在创建DataGridViewCheckBoxColumn 和脚本中的行然后能够从复选框单元格中获取值时遇到此问题?
【问题讨论】:
标签: c# winforms datagridview