Datagridview没有显示正确的rowcount

时间:2015-05-05 07:00:18

标签: vb.net datagridview

我做了一个gridview,其中添加了项目并调用了一个函数来计算数量

将项目添加到列表中后调用

Private Sub totalimsum()
    Dim totalsum As Double = 0
    Dim totalitem As Double = 0

    For i As Integer = 0 To dgvitemlist.Rows.Count - 1
        totalsum += dgvitemlist.Rows(i).Cells("Total").Value
        'totalitem += dgvitemlist.Rows(i).Cells("qty").Value
    Next
    Label16.Text = dgvitemlist.Rows.Count.ToString()
    amttotal = totalsum.ToString()
    txttotal.Text = amttotal
    calctotal()
End Sub

它完美无缺 但是当一行被删除时我再次调用该函数来重新计算金额,但它错误地计算了金额,我发现rowcount比datagridview中存在的行多1,所以我添加了一个新函数并调用它,但我认为它不是解决方案

    Private Sub totalimsumd()
    Dim totalsum As Double = 0
    Dim totalitem As Double = 0

    For i As Integer = 0 To dgvitemlist.Rows.Count - 2
        totalsum += dgvitemlist.Rows(i).Cells("Total").Value
        'totalitem += dgvitemlist.Rows(i).Cells("qty").Value
    Next
    Label16.Text = dgvitemlist.Rows.Count.ToString()
    amttotal = totalsum.ToString()
    txttotal.Text = amttotal
    calctotal()
End Sub

1 个答案:

答案 0 :(得分:0)

尝试重建DataGridView的数据源,这意味着您重新加载DataGridView的内容。

您可以尝试使用dgv.update();和dgv.refresh();删除行时的方法。