0

Simple code lines:

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

Will result in a "System.FormatException: Formatted value of the cell has a wrong type." when the form is executed.

Any idea why? Thanks

Jaime Oliveira
  • 751
  • 1
  • 5
  • 13
  • I've found manually building an unbound DataDridView in code to be very frustrating. I'd just build it in the IDE then use it. You can define checkbox columns to work with. What are you trying to display? - the code doesn't make much sense - you are trying to assign a control to a TextBox value. You should have a Checkbox column and set its value. – rheitzman Oct 27 '15 at 22:10

1 Answers1

0

You are formatting the cell as DataGridViewTextBoxCell at myGrid.Rows(0).Cells("Answers").Value = "0". Change the order of setting cell type and setting value processes like this:

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

Or, you can simply add the "Answers" column as DataGridViewCheckBoxColumn if you are going to set true/false values to all the cells of the column:

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
jhmt
  • 1,401
  • 1
  • 11
  • 15