出现FormatException了未处理vb.net未处理、FormatException、net、vb

2023-09-05 04:14:50 作者:该用户已经成仙

这可能是一个长期的问题,但请你多多包涵一点。

我有一个 dataGridView1的 Form1中的列科code( 电池(2),格式为code(细胞(3))),数量(细胞(5))

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

DGV1

 科code |形式为code |数量
     001 FRM1 10
     002 FRM2 -10
     001 FRM1 20
 
System.FormatException 输入字符串的格式不正确

现在的DGV2让我们说我有相同的分支code和形式code的数据。

DGV2

 科code |形式为code |数量
    001 FRM1 50
    002 FRM2 50
 

现在的$ C $下面依次通过 DGV1 C,那么总结了所有的用相同的表code和科code 将其添加到 DGV2

例如: DGV1结果

 科code |形式为code |数量
    001 FRM1 30
    002 FRM2 -10
 

再经过将其添加到DGV2这会是这个结果。

DGV 2结果

 科code |形式为code |数量
    001 FRM1 80
    002 FRM2 40
 

code

 对于每个xRows在MDIAdjustment.oTransferRows.Where(功能(X)x.Cells(GVPosted)。Value.ToString()=否)

      如果(occurences.ContainsKey(xRows.Cells(3).Value.ToString()))然后

          事件再度发生(xRows.Cells(3).Value.ToString())。数量= Double.Parse(点(xRows.Cells(3).Value.ToString())。的ToString())+ Double.Parse(xRows.Cells (5).Value.ToString())

               其他

                    occurences.Add(xRows.Cells(3).Value.ToString(),新CustomValues​​2用{code = xRows.Cells(3).Value.ToString(),.Branch = xRows.Cells(2)。 Value.ToString(),。说明= xRows.Cells(4).Value.ToString(),.Quantity = Double.Parse(xRows.Cells(5).Value.ToString())})

               结束如果

     下一个
 

声明事件再度发生字典

 昏暗的事件再度发生作为新词典(串,CustomValues​​2)

类CustomValues​​2

公共属性code()作为字符串
公共财产科()作为字符串
公共属性描述()作为字符串
公共财产数量()作为双

末级
 

现在的问题是,当我计算仅使用正整数,我没有问题,但是当我开始添加负数我得到一个错误在这行

 事件再度发生(xRows.Cells(3).Value.ToString())。数量= Double.Parse(点(xRows.Cells(3).Value.ToString())。的ToString())+ Double.Parse(xRows.Cells(5).Value.ToString())
 

出现FormatException是未经处理的输入字符串的不正确的格式

抱歉长后,我试图做的事情,但他们不排除错误。

解决方案

更​​改错误行:

 事件再度发生(xRows.Cells(3).Value.ToString())。数量=事件再度发生(xRows.Cells(3).Value.ToString())。数量+双。解析(xRows.Cells(5).Value.ToString())
 

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

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.

解决方案

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.