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?