Click here to Skip to main content
15,936,482 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview in my C# application which is bound to an array of user defined objects. Some of the data items in my objects are numeric data type - float and uint to be specific. The data gridview has been populated with data and I am doing the cell level data validation.

During validation, anytime I try to convert data by using Convert.ToSingle() or Convert.ToUInt16(), and if the input cell value is "0", I get a "Input String was not in a correct format" exception. But if my cell value is "00" or any other non-zero numeric value, the convert functions (Convert.ToSingle() or Convert.ToUInt16()) are successful.

I do not understand why "0" input value for Convert.ToSingle() or Convert.ToUInt16() will throw a format exception. Any idea what I should do to prevent this exception? I appreciate any help!


I suspect that it's your other code that is the problem - I tried it here with the code below and had no problem. Check you are actually accessing the right cell, and post a code fragment if necessary. It may be worth your using Singe.TryParse rather than Convert.ToSingle, as that reports problems via a bool, rather than excepting on user input.
String s = "0";
Single si = Convert.ToSingle(s);
if (Single.TryParse(s, out si))
Share this answer
Thank you for your response. I tried the piece of code you posted, I still have the exception raised in Convert.ToSingle(s). Then I commented Convert.ToSingle(s) and the TryParse() was successful, when I had the hardcoded string as "0".

But the TryParse() is not successful with the datagridview input as "0". Here is the piece of code, which is giving me the issue:

private void LKDataGridView_CellValidating(object sender,
           DataGridViewCellValidatingEventArgs CellEvent)
    Single TempFloatVal = 0.0f;
    switch ((ELKCol) CellEvent.ColumnIndex)

      case ELKCol.Delta:
         if (Single.TryParse(CellEvent.FormattedValue.ToString(),
                               out TempFloatVal))
             MessageBox.Show("Value of Delta: " +
             MessageBox.Show("Input value: " +


       default:   break


The data type for "Delta" is float. Any time i have non-zero numeric value entered for Delta in datgridview, Single.TryParse() succeeds. Any time I enter a 0 value for Delta in datgridview, Single.TryParse() fails.

I just am not able to figure out what is going on. Any suggestions is very much appreciated!

Share this answer
Samuel Cherinet 11-Aug-10 14:51pm    
on top of everything you might want to check if the value in the cell is not null in the first place,.. it might be so that the cell considers "0" as null. you can check that in the "NullValue" property of the column's Cellstyle. Just something to try ..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900