如何获得每10行的值的总和? [英] How to get sum of the values for every 10 rows?

查看:88
本文介绍了如何获得每10行的值的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Datagridview中有一个列(超过300行),并希望获得每10行的总和。第1列中的数据之间没有间隙。



我只想在另一列中获得每第10行的总和值。我试过向我展示每行累积的#s!



任何帮助都将不胜感激。非常感谢!



我尝试过:



点心整数= 0



For i = 0 To data1.Rows.Count - 1

sum + = data1.Rows(i).Cells (0).Value

data1.Rows(i).Cells(1).Value = sum





如果(i + 1)= 10那么

总和= 0

否则

结束如果



下一步

解决方案

处理RowPrePaint方法:

 私有  Sub  myDataGridView_RowPrePaint( ByVal  sender 作为 对象 ByVal  e  As  DataGridViewRowPrePaintEventArgs)
Dim rowIndex As 整数 = e.RowIndex
Dim sum As 整数 = 0

如果 rowIndex> = 0 AndAlso (rowIndex + 1 Mod 10 = 0 然后
Dim dr As DataGridViewRow

对于 i 作为 整数 = 0 10 - 1
dr = myDataGridView.Rows(rowIndex - i)
sum + = CInt (dr.Cells( 0 )。值)
下一步

dr = myDataGridView.Rows(rowIndex)
dr.Cells( 1 )。值= sum
结束 如果
结束


I have a column in Datagridview (over 300 rows)and would like to get the sum of every 10 rows. There is no gap between the data in Column 1.

I just want to have the sum value for every 10th rows in the other column. What I have tried showing me the cumulative #s in each row!

Any help would be appreciated. Thanks so much!

What I have tried:

Dim sum As integer = 0

For i = 0 To data1.Rows.Count - 1
sum+=data1.Rows(i).Cells(0).Value
data1.Rows(i).Cells(1).Value = sum


If (i+1) = 10 Then
Sum = 0
Else
End If

Next

解决方案

Handle the RowPrePaint method:

Private Sub myDataGridView_RowPrePaint(ByVal sender As Object, ByVal e As DataGridViewRowPrePaintEventArgs)
    Dim rowIndex As Integer = e.RowIndex
    Dim sum As Integer = 0

    If rowIndex >= 0 AndAlso (rowIndex + 1) Mod 10 = 0 Then
        Dim dr As DataGridViewRow

        For i As Integer = 0 To 10 - 1
            dr = myDataGridView.Rows(rowIndex - i)
            sum += CInt(dr.Cells(0).Value)
        Next

        dr = myDataGridView.Rows(rowIndex)
        dr.Cells(1).Value = sum
    End If
End Sub


这篇关于如何获得每10行的值的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆