首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在运行时获取或设置DataGridViewCheckBoxCell值

无法在运行时获取或设置DataGridViewCheckBoxCell值
EN

Stack Overflow用户
提问于 2020-04-18 11:51:56
回答 1查看 137关注 0票数 0

我看过其他关于这个问题的帖子,但是除了一个空字符串之外,我似乎无法从DataGridViewCheckBoxCell中得到一个值。

下面是我尝试在安装新行时设置单元格的真假值的方法,但没有效果。我在运行时创建所有的列和行,这样编辑器就不适用了。

代码语言:javascript
复制
//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 ),都没有效果。

代码语言:javascript
复制
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和行时,能够从复选框单元格中获得值?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-19 00:38:17

修正了这个问题,不得不用".EditedFormatedValue“来回答Can't Identify Values of DataGridViewCheckBoxCell这个问题。

该值似乎仍然是一个空字符串,它只更新到正确的值,并且只在CellLeave事件上触发CellLeave,除非我使用了强制该值更新的.EditedFormatedValue,但在这样做时,由于某种原因,它会触发CellValueChanged两次,但它有效。

编辑:我发现实现它的最好方法是编写

代码语言:javascript
复制
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不是必需的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61288617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档