VB.NET - DataGridView control throws errors when attempting to set cells empty or blank

Hi Experts,

I've got a DataGridView control that includes some columns that are bound to Double fields in my database.

When I edit any of these cells so that they are empty or blank I get this error:

The following exception occurred in the DataGridView:
System.FormatException: Input string was not in a correct format. —> System.FormatException: Input string was not in a correct format.
at System.Number.ParseDoubleString value, NumberStyles options, NumberFormatlnfo numfmt) at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider)
— End of inner exception stack trace — at
System.Windows.Forms.Formatter.lnvokeStringParseMethod( Object value, Type targetType, IFormatProvider formatlnfo) at
System.Windows.Forms.Formatter.ParseObjectlnternal(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatlnfo, Object formattedNullValue) at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatlnfo, Object formattedNullValue,
Object dataSourceNullValue) at
System.Windows.Forms.DataGridViewCell.ParseFormattedValu elnternalfType valueType, Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter) at
System.Windows.Forms.DataGridViewCell.ParseFormattedValu e(Object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter) at
System.Windows.Forms.DataGridView.PushFormattedValuefDa taGridViewCell& dataGridViewCurrentCell, Object formattedValue, Exception& exception)
To replace this default dialog please handle the DataError event.

Open in new window


How can I fix these errors?
I don't really mind if the cells display as empty, blank or 0.

Regards,
Leigh
LVL 1
LeighWardleAsked:
Who is Participating?
 
PatHartmanCommented:
Make sure that the table allows null for the column in question.
0
 
LeighWardleAuthor Commented:
Thanks, Pat.

I did a bit more experimenting.

I found a fix for my problem by opening the DataGridView in the Designer and setting the NullValue to an empty string.

Now I can delete content of any of those cells without the error.
Next time the grid is displayed the deleted cells appear as 0.0

Regards,
Leigh
1
 
PatHartmanCommented:
I would have changed the table definition to allow nulls but that works.  I don't ever allow nullstrings in tables because it means that I need to work differently with numeric/date and text fields.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.