1

I'm using this code to fill/format a DataGridView. The idea is, if the user checks a checkbox and later hits a save button it writes either the TrueValue or the FalseValue back to the database. The code works fine except for an issue with the FalseValue when the DataGridView loads. If all the values in the database are the TrueValue the DataGridView opens fine, all the checkboxes are checked. The user can uncheck, check, whatever....then save, the appropriate TrueValue or FalseValue is written to the database accordingly. The problem is, if there is a FalseValue in the database, when the DataGridView loads (or you hold the pointer over the unchecked checkbox) it raises the DataGridView Default Error Dialog. The error is:

The following exception occurred in the DataGridView: "System.FormatException: is not a valid value for Boolean. ---> System.FormatException: String was not recognized as a valid Boolean. at System.Boolean.Parse(String value) at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) --- End of inner exception stack trace --- at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at System.Windows.Forms.Formatter.FormatObjectInternal(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue) at System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue) at System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)" To replace this default dialog please handle the DataError event.

My code looks something like this:

Me.dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                Me.dgv.EnableHeadersVisualStyles = False
                Me.dgv.Columns.Clear()
                Me.dgv.AutoGenerateColumns = False
                Me.dgv.Columns.Add(New DataGridViewTextBoxColumn With {.DataPropertyName = "Column_one", .HeaderText = "one", .ReadOnly = True})
                Me.dgv.Columns.Add(New DataGridViewTextBoxColumn With {.DataPropertyName = "Column_two", .HeaderText = "two", .ReadOnly = True})
                Me.dgv.Columns.Add(New DataGridViewCheckBoxColumn With {.DataPropertyName = "Column_three", .HeaderText = "three", .TrueValue = "THR", .FalseValue = ""})
                Me.dgv.Columns.Add(New DataGridViewCheckBoxColumn With {.DataPropertyName = "Column_four", .HeaderText = "four", .TrueValue = "FO", .FalseValue = ""})
                Me.dgv.Columns.Add(New DataGridViewCheckBoxColumn With {.DataPropertyName = "Column_five", .HeaderText = "five", .TrueValue = "FI", .FalseValue = ""})
                Me.dgv.Columns.Add(New DataGridViewCheckBoxColumn With {.DataPropertyName = "Column_six", .HeaderText = "six", .TrueValue = "SI", .FalseValue = ""})
                Me.dgv.Columns.Add(New DataGridViewCheckBoxColumn With {.DataPropertyName = "Column_seven", .HeaderText = "seven", .TrueValue = "SEV", .FalseValue = ""})
                Me.dgv.Columns.Add(New DataGridViewCheckBoxColumn With {.DataPropertyName = "Column_eight", .HeaderText = "8", .TrueValue = "EI", .FalseValue = ""})
                Me.dgv.Columns.Add(New DataGridViewCheckBoxColumn With {.DataPropertyName = "Column_nine", .HeaderText = "9", .TrueValue = "NINE", .FalseValue = ""})
                For i = 0 To Me.dgv.Columns.Count - 1
                    If i >= 2 Then
                        Me.dgv.Columns(i).HeaderCell.Style.BackColor = Color.Yellow
                    End If
                Next
                Me.dgv.DataSource = dt

I'm not getting why the TrueValue works fine but the FalseValue isn't. I've tried FalseValue strings other than "" with the same result. Is there some obvious problem with what I'm trying to do or a way around raising the error?

user3799279
  • 143
  • 1
  • 11
  • 2
    Check your database entries. It sounds like the saved `False` value is not what you're expecting it to be. Even saving `False` as `'_'` where _ is some form of white space (like a normal spacebar or even ascii character `0x02` for example) will throw the error you're seeing. If the `False` db value is anything other than `""` or `System.DBNull.Value`, this error occurs. – OhBeWise Jul 24 '15 at 19:27
  • @OhBeWise....thank you! I double-checked the db and the values were not actually "" but rather variations of " ", " ", you get the picture. I looked at the field datatypes and they were char. I changed them to varchar and it works like a charm. Thanks! – user3799279 Jul 25 '15 at 00:13

0 Answers0