2

这可能是一个很长的问题,但请耐心等待。

Datagridview1Form1列有意见BranchCode("cells(2) ,FormCode("cells(3)")), Quantity("cells(5)")

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

DGV1

 BranchCode | FormCode | Quantity
     001         Frm1       10
     002         Frm2      -10
     001         Frm1       20

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

DGV2

BranchCode | FormCode | Quantity
    001        Frm1        50
    002        Frm2        50

现在下面的代码循环然后在添加之前DGV1总结所有Quantity相同 的FormCode and BranchCodeDGV2

例如:DGV1 结果

BranchCode | FormCode | Quantity
    001        Frm1        30
    002        Frm2       -10

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

DGV 2 结果

BranchCode | FormCode | Quantity
    001        Frm1        80
    002        Frm2        40

代码

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

宣言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

对不起,对于长篇文章,我已经尝试过做一些事情,但他们并没有消除错误。

4

1 回答 1

2

将您的错误行更改为:

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

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

于 2012-12-06T05:23:34.057 回答