FormatException未处理vb.net

时间:2012-12-06 03:55:47

标签: .net vb.net winforms exception datagridview

这可能是一个很长的问题,但请稍微跟我说。

Datagridview1 Form1 BranchCode("cells(2) ,FormCode("cells(3)")), Quantity("cells(5)") BranchCode | FormCode | Quantity 001 Frm1 10 002 Frm2 -10 001 Frm1 20

现在让我们说这是它的样子

DGV1

BranchCode | FormCode | Quantity
    001        Frm1        50
    002        Frm2        50

现在在DGV2上,假设我有一个具有相同分支代码和表单代码的数据

DGV2

DGV1

现在,下面的代码循环遍历Quantity,然后将所有FormCode and BranchCodeDGV2相加,然后再添加到BranchCode | FormCode | Quantity 001 Frm1 30 002 Frm2 -10

Ex: DGV1结果

BranchCode | FormCode | Quantity
    001        Frm1        80
    002        Frm2        40

然后将其添加到DGV2后,这将是结果

DGV 2结果

For Each xRows In MDIAdjustment.oTransferRows.Where(Function(x) x.Cells("GVPosted").Value.ToString() = "No")

      If (occurences.ContainsKey(xRows.Cells(3).Value.ToString())) Then

          occurences(xRows.Cells(3).Value.ToString()).Quantity = Double.Parse(occurences(xRows.Cells(3).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(5).Value.ToString())

               Else

                    occurences.Add(xRows.Cells(3).Value.ToString(), New CustomValues2 With {.Code = xRows.Cells(3).Value.ToString(), .Branch = xRows.Cells(2).Value.ToString(), .Description = xRows.Cells(4).Value.ToString(), .Quantity = Double.Parse(xRows.Cells(5).Value.ToString()) })  

               End If

     Next

CODE

occurences dictionary

Dim occurences As New Dictionary(Of String, CustomValues2) Class CustomValues2 Public Property Code() As String Public Property Branch() As String Public Property Description() As String Public Property Quantity() As Double End Class

的声明
 occurences(xRows.Cells(3).Value.ToString()).Quantity = Double.Parse(occurences(xRows.Cells(3).Value.ToString()).ToString()) + Double.Parse(xRows.Cells(5).Value.ToString())

问题是,当我只使用正整数进行计算时,我没有问题,但是当我开始添加负数时,我在这一行上出错了

FormatException was Unhandled" Input string was not in a correct format

说{{1}}

抱歉对于长篇帖子,我试图做一些事情,但他们没有删除错误。

1 个答案:

答案 0 :(得分:2)

将您的错误行更改为:

occurences(xRows.Cells(3).Value.ToString()).Quantity = occurences(xRows.Cells(3).Value.ToString()).Quantity + Double.Parse(xRows.Cells(5).Value.ToString())

您需要添加数量属性,因此需要获取其当前值。