FormatException was unhandled vb.net

不问归期 提交于 2019-12-24 02:57:13

问题


This Might be a long question but please do bear with me a little.

I have a Datagridview1 on Form1 with the columns BranchCode("cells(2) ,FormCode("cells(3)")), Quantity("cells(5)")

Now Let's say this is how it looks like

DGV1

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

Now on DGV2 let's say I have an data with the same branch code and form code

DGV2

BranchCode | FormCode | Quantity
    001        Frm1        50
    002        Frm2        50

Now the code below loops through DGV1 then sums up all the Quantity with the same FormCode and BranchCode before adding it to DGV2

Ex: DGV1 results

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

Then After adding it to DGV2 this would be the result

DGV 2 Results

BranchCode | FormCode | Quantity
    001        Frm1        80
    002        Frm2        40

CODE

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

Declaration of 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

The question is, when I compute using only positive integers, I have no problem, but when I start to add negative numbers I get an error on this line

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

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

Sorry For the long Post, I've tried to do things but they don't remove the error.


回答1:


Change your error line to :

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

You need to add to the quantity property, so you need to fetch its current value.



来源:https://stackoverflow.com/questions/13736693/formatexception-was-unhandled-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!