【问题标题】:Can't get or set DataGridViewCheckBoxCell values at runtime无法在运行时获取或设置 DataGridViewCheckBoxCell 值
【发布时间】: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


    【解决方案1】:

    已修复,必须使用此问题Can't Identify Values of DataGridViewCheckBoxCell 的答案中的“.EditedFormatedValue”。

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

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

    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 上触发CellContentClick。这是双赢,不会质疑。

    顺便说一句,不需要设置cell.FalseValue 和cell.TrueValue。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多