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.

RePierre
  • 9,358
  • 2
  • 20
  • 37
Miko
  • 363
  • 1
  • 8
  • 22

4 Answers4

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.

Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
  • 1
    Yeah, tryparse helped me to identify where I went wrong. Thank you. – Miko Feb 23 '15 at 17:19
  • 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 Feb 25 '15 at 01:06
  • @dustinmoris Are you high or what :D ? – Kavindu Dodanduwa Feb 25 '15 at 02:10
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

dustinmoris
  • 3,195
  • 3
  • 25
  • 33
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 + " £";
        }
    }
}
Hiral Nayak
  • 1,062
  • 8
  • 15
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

Community
  • 1
  • 1
Rohit
  • 10,056
  • 7
  • 50
  • 82