Question summation in datagridview

fugio

New member
Joined
Jul 21, 2013
Messages
1
Programming Experience
Beginner
I created a datagridview and
Added number of rows
from 1 to 7
summation to results
1
2
3
4
5
6
7
=> results
1
3
6
10
15
21
28
(1+2=3; 3+3=6; 6+4=10; 10+5=15; 15+6=21; 21+7=28)
write simple way
VB.NET:
[/FONT][/COLOR]DataGridView1.RowCount = 9        For i As Integer = 1 To 7
            DataGridView1.Rows(i).Cells(0).Value = i
        Next
        Dim kq1 As Integer = 0
        Dim kq2 As Integer = 0
        Dim kq3 As Integer = 0
        Dim kq4 As Integer = 0
        Dim kq5 As Integer = 0
        Dim kq6 As Integer = 0
        Dim kq7 As Integer = 0


        Dim a As Integer = DataGridView1.Rows(0).Cells(0).Value
        Dim b As Integer = DataGridView1.Rows(1).Cells(0).Value
        Dim c As Integer = DataGridView1.Rows(2).Cells(0).Value
        Dim d As Integer = DataGridView1.Rows(3).Cells(0).Value
        Dim e1 As Integer = DataGridView1.Rows(4).Cells(0).Value
        Dim f As Integer = DataGridView1.Rows(5).Cells(0).Value
        Dim g As Integer = DataGridView1.Rows(6).Cells(0).Value
        kq1 = a + b
        kq2 = c + kq1
        kq3 = d + kq2
        kq4 = e1 + kq3
        kq5 = f + kq4
        kq6 = g + kq5


        DataGridView1.Rows(0).Cells(0).Value = kq1
        DataGridView1.Rows(1).Cells(0).Value = kq2
        DataGridView1.Rows(2).Cells(0).Value = kq3
        DataGridView1.Rows(3).Cells(0).Value = kq4
        DataGridView1.Rows(4).Cells(0).Value = kq5

        DataGridView1.Rows(5).Cells(0).Value = kq6[COLOR=#434343][FONT=helvetica]
if adding new rows can't controlled
please help with all of the more compact form
 
LINQ is perfect for doing this sort of thing. If you want to sum all the Integer values in a column of a DataGridView then you can do it like this:
Dim sum = myDataGridView.Rows.Cast(Of DataGridViewRow)().Sum(Function(row) CInt(row.Cells(0).Value))
 
Back
Top