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
解决方案
使用大量 .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
推荐阅读
- html - 尝试借助 reactjs 中的锚标记重定向页面
- javascript - CSS滚动球动画位置
- python - 获取错误为:期望值:尝试使用请求获取数据时的第 1 行第 1 列(字符 0)
- c# - 如何在没有等待的情况下计算请求时间
- javascript - DESC 中的订单记录在 SequelizeJS 中不起作用
- scala - Embedded Kafka & Spark 2.3 版本不匹配问题
- azure - 如何更改默认答案 Azure qnamaker 中未找到 QnAMaker 答案
- google-cloud-platform - 如何在 BigQuery 控制台中加载 Google ClusterData 2019 跟踪?
- java - 是否可以比较毫秒级?比如 00:05 秒 > 00:002 秒
- amazon-s3 - 网页存档到 mht/mntml