首页 > 解决方案 > “是否有在鼠标指针选择时查找 DataGridView 中列值总和的功能?”

问题描述

我可以通过将鼠标拖动到它们上来计算 DataGridView 中列的选定单元格值的总和。

我尝试了以下代码,但它对我不起作用。

`Private Sub grid1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grid1.SelectionChanged

Try
        Dim sum As Double = 0
        For Each row As DataGridViewRow In grid1.SelectedRows
            sum += Convert.ToDouble(row.Cells("Item Rate").Value)
           Next
                       txtCr.TextboxValue = sum.ToString()
    Catch ex As Exception
    End Try
End Sub

标签: vb.net

解决方案


多亏了吉米

 Private Sub grid1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grid1.SelectionChanged
    Try
        Dim SUM As Double = 0
        For Each cell As DataGridViewCell In grid1.SelectedCells
            If cell.ColumnIndex() Then
                SUM += Convert.ToDouble(cell.Value)
            End If
        Next
        txtCr.TextboxValue = SUM.ToString()
    Catch ex As Exception
    End Try
End Sub

结束类


推荐阅读