Conversion from string "" to type 'Double' is not valid [closed]

-2

Good morning

I am trying to add the values of a gridview that I select to a texbox, create the following (see code below) but it returns this error "Conversion from string" "to type 'Double' is not valid."

Code

Private Sub SumOpcion2 ()

    Dim total As Decimal = 0
    For Each row As GridViewRow In Gvcobranzas.Rows
        total += row.Cells(4).Text
    Next
    TxtMonto.Text = total


End Sub
    
asked by neryad 23.08.2017 в 15:28
source

2 answers

0

Since the error marks you with an empty string ( "" ) you can try it with this code:

Dim total As Decimal = 0
For Each row As GridViewRow In Gvcobranzas.Rows
    Dim x As Decimal = 0
    If Decimal.TryParse(row.Cells(4).Text, x) Then
        total += x
Next
TxtMonto.Text = total

Using TryParse (reference) the attempt to convert the text into number (it is necessary to declare the variable before of the TryParse ) and if the conversion is successful, then you do the addition; if it is not successful, the program continues.

    
answered by 23.08.2017 / 15:57
source
0

You must use the Convert.ToDouble() method to convert the value of the cell to Double :

Dim total As Decimal = 0
For Each row As GridViewRow In Gvcobranzas.Rows
    total += Convert.ToDouble(row.Cells(4).Text)
Next
TxtMonto.Text = total

I hope it helps you.

    
answered by 23.08.2017 в 15:34