当我尝试运行该程序来计算付款和总利息时,出现“从字符串“”转换为“ Double”类型无效”。

时间:2019-05-14 04:50:23

标签: vb.net

Private Sub TxtAmt_Enter(sender As Object, e As EventArgs) Handles TxtAmt.Enter
    Dim Amount As Integer
    For index As Integer = 2 To DataGridView1.RowCount - 1
        Amount += Convert.ToInt32(DataGridView1.Rows(index).Cells(3).Value)

        'if you have the other column to get the result you  could add a new one like these above (just change Cells(2) to the one you added)
    Next

    Dim tcredit As Decimal = 0
    tcredit = Convert.ToDecimal(DataGridView1.Rows(0).Cells(4).Value)
    Dim total As Double = 0
    If DataGridView1.Rows.Count > 2 Then

        For i As Integer = 1 To DataGridView1.RowCount - 2
            total = total + Convert.ToDecimal(DataGridView1.Rows(i).Cells(3).Value)
        Next
        total = total + TxtAmt.Text
        Dim row As String() = New String() {Format(DateTimePicker1.Value, "yyyy-MM-dd"), "Payments", TxtVoucher.Text, TxtAmt.Text, (Convert.ToString(DataGridView1.Rows(0).Cells(4).Value - total)), CmboInvoc.Text}
        DataGridView1.Rows.Add(row)
    Else
        Dim row As String() = New String() {Format(DateTimePicker1.Value, "yyyy-MM-dd"), "Payments", TxtVoucher.Text, TxtAmt.Text, (Convert.ToString(DataGridView1.Rows(0).Cells(4).Value - TxtAmt.Text)), CmboInvoc.Text}
        DataGridView1.Rows.Add(row)
        total = TxtAmt.Text
    End If

    TxtDbit.Text = total
    TxtBalnc.Text = (tcredit) - Convert.ToDecimal(TxtDbit.Text)

End Sub

1 个答案:

答案 0 :(得分:0)

使用许多.TryParse方法修复了代码。现在,它将在Option Strict On(启用严格选项)下工作。

Private Sub TxtAmt_Enter(sender As Object, e As EventArgs) Handles TxtAmt.Enter
    Dim Amount As Integer
    For index As Integer = 2 To DataGridView1.RowCount - 1
        Dim AmountToAdd As Integer
        Integer.TryParse(DataGridView1.Rows(index).Cells(3).Value.ToString, AmountToAdd)
        Amount += AmountToAdd
    Next

    Dim tcredit As Decimal
    Decimal.TryParse(DataGridView1.Rows(0).Cells(4).Value.ToString, tcredit)
    Dim total As Double = 0
    If DataGridView1.Rows.Count > 2 Then
        For i As Integer = 1 To DataGridView1.RowCount - 2
            Dim rowTotal As Double
            Double.TryParse(DataGridView1.Rows(i).Cells(3).Value.ToString, rowTotal)
            total += rowTotal
        Next
        Dim AmountFromTextBox As Double
        Double.TryParse(TxtAmt.Text, AmountFromTextBox)
        total += AmountFromTextBox
        Dim row As String() = New String() {Format(DateTimePicker1.Value, "yyyy-MM-dd"), "Payments", TxtVoucher.Text, TxtAmt.Text, (Convert.ToString(DataGridView1.Rows(0).Cells(4).Value - total)), CmboInvoc.Text}
        DataGridView1.Rows.Add(row)
    Else
        Dim row As String() = New String() {Format(DateTimePicker1.Value, "yyyy-MM-dd"), "Payments", TxtVoucher.Text, TxtAmt.Text, (Convert.ToString(DataGridView1.Rows(0).Cells(4).Value - TxtAmt.Text)), CmboInvoc.Text}
        DataGridView1.Rows.Add(row)
        Dim AmountFromTextBox As Double
        Double.TryParse(TxtAmt.Text, AmountFromTextBox)
        total = AmountFromTextBox
    End If

    TxtDbit.Text = total.ToString
    TxtBalnc.Text = (tcredit - Convert.ToDecimal(TxtDbit.Text)).ToString

End Sub