0

I'm having problems with error: input string was not in a correct format. I'm trying to convert curency in datagrid. At point where I get error (this is where I set value to value variable) text variable haves value 22.22 so I don't know what is wrong with format.

public void valute()
{
    int rowCount = dataGridView1.RowCount;
    decimal value = 0;
    for (int i = 0; i < rowCount; i++)
    {
        string text = dataGridView1.Rows[i].Cells[3].Value.ToString();

        if (evro_check.Checked)
            dataGridView1.Rows[i].Cells[3].Value = text + " €";
        else if (dolar_check.Checked)
        {
            value = Decimal.Parse(text.Replace(',', '.'), CultureInfo.InvariantCulture);
            dataGridView1.Rows[i].Cells[3].Value = value.ToString() + " $";
        }
        else
        {
            dataGridView1.Rows[i].Cells[3].Value = value + " £";
        }
    }
}

EDIT: Right now I'm just adding curency sign and later I'll also change € to $ and thats way I'm using additional variable (value) and not using text for other 2 currencys.

9
  • 2
    Are you sure there aren't any spaces or anything similar in the string? Also note that your title talks about Double.Parse, but your code uses Decimal.Parse... Commented Feb 23, 2015 at 8:18
  • 1
    If you are going to place the value in grid's cell as text, why do you convert it to the decimal in the first place? Commented Feb 23, 2015 at 8:18
  • 1
    I need to convert it to decimal because I'll need to multiplay it with currency exchanges. Commented Feb 23, 2015 at 8:20
  • 1
    What is the value of text exactly? Debug your code and tell us. Commented Feb 23, 2015 at 8:25
  • 1
    I'm guessing since you are iterating over all rows, maybe you have an empty cell. Decimal.Parse on empty string throws an error if I remember correctly. Commented Feb 23, 2015 at 8:31

4 Answers 4

2

The best option you have is to use Tryparse over Parse

TryParse

This overload differs from the Decimal.Parse(String) method by returning a Boolean value that indicates whether the parse operation succeeded instead of returning the parsed numeric value. It eliminates the need to use exception handling to test for a FormatException in the event that s is invalid and cannot be successfully parsed.

A suggestion to improve the code

string text = dataGridView1.Rows[i].Cells[3].Value.ToString();
Decimal value=0;

if (Decimal.TryParse(text.Replace(',', '.'), out value))
{
   //parse success
   dataGridView1.Rows[i].Cells[3].Value = value.ToString() + " $"; // put the correct value
}
else {
   //parse not success 
   dataGridView1.Rows[i].Cells[3].Value ="- $"; // Put something which allow you to identify the issue.
 }

This will allow you to identify where you have wrongly formatted values in data grid.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, tryparse helped me to identify where I went wrong. Thank you.
Seriously? Suggesting to debug your application via screen output is a terrible answer! Why wouldn't you just put a breakpoint into the line where it failed to debug the cause? This is completely backwards! VisualStudio has probably the best debugger in the industry and you decided to abandon it????
@dustinmoris Are you high or what :D ?
1

Be careful with cultures. For example in the UK this "10,000.10" is 10 thousand and 1/10 of your currency, while in Germany the format would be the other way around: "10.000,10".

What you do is replacing all "," with an ".". Unless you changed the current culture of your application to a format where this makes sense, then this will obviously end up with a FormatException.

You should set the CultureInfo to the culture which you are targeting.

https://msdn.microsoft.com/en-US/library/b28bx3bh%28v=vs.80%29.aspx https://msdn.microsoft.com/en-us/library/bz9tc508%28v=vs.140%29.aspx

Also, it would be better to use a format provider, which will format you a correct monetary string according to the specified culture:

decimal value = 10000;

value.ToString("C", CultureInfo.InvariantCulture);
// Output : ¤10,000.00

value.ToString("C", CultureInfo.GetCultureInfo("de-DE"));
// Output : 10.000,00 €

value.ToString("C", CultureInfo.GetCultureInfo("en-US")).Dump();
// Output: $10,000.00

If you notice, the American format puts the currency symbol at the front, and the German one is at the end. You didn't account for any of these things either.

See: https://msdn.microsoft.com/en-us/library/0b22a4x2%28v=vs.110%29.aspx

Comments

0

try this

public void valute()
{
    int rowCount = dataGridView1.RowCount;
    decimal value = 0;
    for (int i = 0; i < rowCount; i++)
    {
        string text = dataGridView1.Rows[i].Cells[3].Value.ToString();

        if (evro_check.Checked)
            dataGridView1.Rows[i].Cells[3].Value = text + " €";
        else if (dolar_check.Checked)
        {
            if (text != "" || text != "&nbsp;")
            {
                value = Decimal.Parse(text.Replace(',', '.'), CultureInfo.InvariantCulture);
                dataGridView1.Rows[i].Cells[3].Value = value.ToString() + " $";
            }
        }
        else
        {
            dataGridView1.Rows[i].Cells[3].Value = value + " £";
        }
    }
}

Comments

0

You can make use of Culture Info class.

  public void valute()
  {
   int rowCount = dataGridView1.RowCount;
   decimal value = 0;
   for (int i = 0; i < rowCount; i++)
   {
       string text = dataGridView1.Rows[i].Cells[3].Value.ToString();

       if (evro_check.Checked)
           dataGridView1.Rows[i].Cells[3].Value = text.ToString("C", CultureInfo.GetCultureInfo("fr")); //€
       else if (dolar_check.Checked)
       {
           value = Decimal.Parse(text.Replace(',', '.'), CultureInfo.InvariantCulture);
           dataGridView1.Rows[i].Cells[3].Value = text.ToString("C", CultureInfo.GetCultureInfo("fr-CA")); //$
       }
       else
       {
           dataGridView1.Rows[i].Cells[3].Value = text.ToString("C", CultureInfo.GetCultureInfo("en-GB"));
       }
   }
  }

Second Approach make use of Unicode

public static char HexToChar(string hex)
{
  return (char)ushort.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}

\u00A3 is the Pound sign, £

\u20AC is the Euro sign, €.

\u0024 is the dollar sign, $.

How to insert a Symbol (Pound, Euro, Copyright) into a Textbox

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.