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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PatHartmanCommented:
Make sure that the table allows null for the column in question.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.