如果项目已存在于数据网格视图选定列表中,则添加数量

时间:2017-10-15 06:45:54

标签: vb.net

如果datagridview2中已存在item

,我在datagridview2数量单元格上添加数量时遇到问题

我有两个datagridviews。

  1. Datagridview1包含项目列表的价格和数量

  2. Datagridview2包含客户选择的数量和价格的商品。

  3. 我在datagridview中检查项目的代码是:

    If DataGridView2.Rows.Count > 0 Then
    
        Dim Found As Boolean = False
    
        For Each rw1 As DataGridViewRow In DataGridView1.SelectedRows
    
            For Each rw2 As DataGridViewRow In DataGridView2.Rows
    
                If rw1.Cells("Item").Value = rw2.Cells("Column2").Value Then
    
                    rw2.Cells("Column5").Value = rw2.Cells("Column5").Value + 1
                    Found = True
    
                ElseIf Not Found Then
    
                    For Each dr In Me.DataGridView1.SelectedRows
    
                        Me.DataGridView2.Rows.Add(rw1.Cells(1).Value, 1, rw1.Cells(3).Value)
    
                    Next
                    Exit For
                End If                  
            Next  
        Next
    End If
    

    当我第一次选择行时,检查行并添加数量,但是当我第二次选择它时,它不会检查并只添加行到datagridview。

    我的申请表格如下:

    Image shows that the when I select second time it doesn't check and just add row to datagridview debug result. That shows that rw2.cell contains the value of Row 1. and it is not changing, thats why the condition remain false.

    我修复了我的代码:

        Dim sum As Integer = 0
        Dim Found As Boolean = False
        For Each rw1 As DataGridViewRow In DataGridView1.SelectedRows
            For Each rw2 As DataGridViewRow In DataGridView2.Rows
    
                If rw1.Cells("Item").Value = rw2.Cells("Column2").Value Then
    
                    rw2.Cells("Column5").Value = rw2.Cells("Column5").Value + 1
                    Found = True
                    'Subtracting Quantity from Datagridview1
                    For Each sqt As DataGridViewRow In DataGridView1.SelectedRows
                        sqt.Cells(2).Value = sqt.Cells(2).Value - 1
                    Next
                    'calculating Amount
                    For Each row As DataGridViewRow In DataGridView2.Rows
                        row.Cells("Column6").Value = row.Cells("Column5").Value * row.Cells("Column3").Value
                    Next
                    ' Calculating Total Amount
                    '  Dim sum As Integer = 0
                    For i As Integer = 0 To DataGridView2.Rows.Count() - 1 Step +1
                        sum = sum + DataGridView2.Rows(i).Cells("Column6").Value
                    Next
                    TextBox1.Text = sum.ToString()
                    Exit Sub
                End If
            Next
        Next
        For Each dr In Me.DataGridView1.SelectedRows
            Me.DataGridView2.Rows.Add(dr.Cells(1).Value, 1, dr.Cells(3).Value) 
        Next
    

0 个答案:

没有答案