0
string str = e.Row.Cells[index].Text;

int value = Int32.Parse(str, NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo);
if (value >= 100)
    e.Row.Cells[index].BackColor = System.Drawing.Color.Green;

Cell values are 168,88 - 125,45 - 75,3

After parsing str returns 16888 - 12545 - 753 , so all of the cells are set as green

how can i compare real values.?

lilith
  • 1
  • 1
  • 2
    What do you mean by "real values"? What do you *expect* the values to be? Are these actually *non-integer* values with comma as the decimal point? – Jon Skeet Dec 06 '11 at 15:11
  • 2
    You aren't using Convert.ToInt32(), but Int32.Parse(). Also, you allow for thousands separator under an invariant culture, which basically tells the parser to more or less ignore the comma sign. If you are in a culture where the comma is the decimal sign (like Swedish), you should pass the correct CultureInfo to the Parse() call. – Anders Marzi Tornblad Dec 06 '11 at 15:15
  • Somewhat a duplicate of http://stackoverflow.com/questions/831727/c-sharp-decimal-parse-issue-with-commas – Larry Smithmier Dec 06 '11 at 15:17
  • Are you sure you want to parse numbers with a decimal component into integers? – Oded Dec 06 '11 at 15:18
  • i mean 168,88 - 125,45 - 75,3 as real values. i want to get these values from gridview and then compare if they are bigger than 100 – lilith Dec 06 '11 at 15:21
  • Oh... By "real values", do you mean floats? – Anders Marzi Tornblad Dec 06 '11 at 15:28

2 Answers2

4

You are using NumberFormatInfo.InvariantInfo. This treats , as thousands separator.

Are you sure this is the correct one? Did you mean to use something like CultureInfo.GetCulture("fr-FR"), where the , is the decimal separator?

Additionally, if you need to preserve the decimal part, why parse to an integer?

This should work better for you:

decimal.Parse(str, NumberStyles.AllowThousands, CultureInfo.GetCulture("fr-FR"));
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

I think what you're looking for is:

int value = Int32.Parse(str, NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);

The NumberFormatInfo tells the Parse function HOW the input should be interpreted. The InvariantInfo reads as Gets the default read-only NumberFormatInfo that is culture-independent (invariant) from msdn.

DanTheMan
  • 3,277
  • 2
  • 21
  • 40