首页 > 解决方案 > 当我尝试运行程序来计算付款和总利息时,我得到“从字符串转换”到类型“双”无效。

问题描述

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

标签: vb.net

解决方案


使用大量 .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

推荐阅读