[RESOLVED] Correcting null values in boolean columns
I've got a datagridview populating from a database and, there's no way around it, the database will sometimes return null values in a boolean column, and I get the following error:
Quote:
The following exception occurred in the DataGridView:
System.FormatException: Value '' cannot be converted to type 'Boolean'.
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, DataGridViewErrorContexts context)
To replace this default dialog please handle the DataError event.
I tried handling this in the Cellvalidating event, but it didn't work. How do I actually catch those nulls before they cause an error?
Re: Correcting null values in boolean columns
The column supports that situation without any extra code from you.
http://msdn.microsoft.com/en-us/libr...hreestate.aspx
Re: Correcting null values in boolean columns
As the error occurs in the situation when you want to put "something" (of type whatever) into a position (like a variable, datagridcell ...) where a specific type is expected, I would check the type of "something" just in this situation. If it doesn't fit you can give a DEFAULT value.
Re: Correcting null values in boolean columns
(I just went ahead and changed null values to false in the cellformatting event.)