添加DataGridViewCheckBoxCell

时间:2015-10-27 14:14:08

标签: winforms datagridview datagridviewcheckboxcell

简单的代码行:

myGrid.Columns.Add("Question", "Question")
myGrid.Columns.Add("Answers", "Answers")

myGrid.Rows.Add()

myGrid.Rows(0).Cells("Question").Value = "dummy checkbox test"

Dim chk As New DataGridViewCheckBoxCell
chk.TrueValue = "1"
chk.FalseValue = "0"
myGrid.Rows(0).Cells("Answers").Value = "0"
myGrid.Rows(0).Cells("Answers") = chk

将导致“System.FormatException:单元格的格式化值类型错误。”表格执行时。

知道为什么吗? 感谢

1 个答案:

答案 0 :(得分:0)

您正在DataGridViewTextBoxCell将单元格格式化为myGrid.Rows(0).Cells("Answers").Value = "0"。更改设置单元格类型的顺序和设置值过程,如下所示:

myGrid.Rows(0).Cells("Answers") = chk
myGrid.Rows(0).Cells("Answers").Value = "0"

或者,您只需添加"答案"如果您要为列的所有单元格设置true / false值,则列为DataGridViewCheckBoxColumn

myGrid.Columns.Add("Question", "Question")
Dim chkColumn As New DataGridViewCheckBoxColumn
chkColumn.Name = "Answers"
chkColumn.HeaderText = "Answers"
myGrid.Columns.Add(chkColumn)

myGrid.Rows(0).Cells("Question").Value = "dummy checkbox test"
myGrid.Rows(0).Cells("Answers").Value = false