我看过其他关于这个问题的帖子,但是除了一个空字符串之外,我似乎无法从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;
}我还看到了一种设置列本身的真假值的方法,如果它确实设置了所有后续单元格的值,这将是理想的,但这似乎也没有任何作用。
是否还有其他人在创建脚本中的DataGridViewCheckBoxColumn和行时,能够从复选框单元格中获得值?
发布于 2020-04-19 00:38:17
修正了这个问题,不得不用".EditedFormatedValue“来回答Can't Identify Values of DataGridViewCheckBoxCell这个问题。
该值似乎仍然是一个空字符串,它只更新到正确的值,并且只在CellLeave事件上触发CellLeave,除非我使用了强制该值更新的.EditedFormatedValue,但在这样做时,由于某种原因,它会触发CellValueChanged两次,但它有效。
编辑:我发现实现它的最好方法是编写
Program.mainForm.TableMainGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = Convert.ToBoolean(Program.mainForm.TableMainGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue);在我的CellContentClick事件中触发CellValueChanged。奇怪的是,它似乎也停止了在CellLeave上触发CellLeave。这是一场胜利,而不是质疑。
设置cell.FalseValue & cell.TrueValue不是必需的。
https://stackoverflow.com/questions/61288617
复制相似问题