Domanda

I´m trying to parse a String to a Decimal but I keep getting a formatException

Dim row as GridViewRow
    for each row in grdActieRittenActiefAlt.rows
        Dim rbl as RadioButtonList = row.FindControl("tblAddAct")
        'toevoegen = true
        if rbl.SelectedItem.Value = true then
            Dim opgaveIdent as Integer = Convert.ToInt32(grdActieRittenActiefAlt.DataKeys(row.RowIndex).Value.ToString())

            Dim curOldTarief as Decimal = Convert.ToDecimal(lblCuratiefTarief.Text)
            Dim curOldKorting as Decimal = Convert.ToDecimal(lblCuratiefKorting.Text)

            Dim curTariefString as String = row.Cells(4).Text
            Dim curKortingString as String = row.Cells(6).Text

            Dim curTarief as Decimal = Convert.ToDecimal(curTariefString)
            Dim curKorting as Decimal = Convert.ToDecimal(curKortingString)

            lblCuratiefTarief.Text = (curOldTarief + curTarief).ToString()
            lblCuratiefKorting.Text = (curOldKorting + curKorting).ToString()


        end if
    next row

The input is 431,25.

So far I've tried the following:

  • Changing the comma to a colon using .Replace(",",".") => Didn't work
  • Using a forced CultureInfo => Didn't work
  • Use the row.Cells(4).Text directly => Didn't work
  • Use a substring query to get only the round numbers (431) => Worked but is no solution
  • Does anyone else have any suggestions?

    È stato utile?

    Soluzione

    After doing a lot more testing and trying I've found a workaround/solution for the problem.

    The row.Cells(4).Text was a boundfield. I've changed that to a TemplateField with a label. By making a local variable (Label) and using that to convert the decimal from it works. I got no idea why but for me it's a solution for now.

    Altri suggerimenti

    Have you tried the FormatNumber function?

    FormatNumber(number, 2)
    

    2 is the number of decimal places.

    Autorizzato sotto: CC-BY-SA insieme a attribuzione
    Non affiliato a StackOverflow
    scroll top