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

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

if adding new rows can't controlled
please help with all of the more compact form

Recommended Answers

All 4 Replies

Use a for loop with elements of line 12 to line 32 of your code.

Maybe it would be better to start off by setting up the datagridview1 correctly at the form load event and after using a button to do whatever you want to do. Also you need to add rows to the datagridview1:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim row As String = ""
        For i As Integer = 0 To 6
            DataGridView1.Rows.Add(row)
        Next
        For i As Integer = 1 To 7
            DataGridView1.Rows(i - 1).Cells(0).Value = i
        Next
End Sub

Agreed with @ddanbe, Everything could be done in a for loop at Form Load() event.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        For i As Integer = 0 To 6

            DataGridView1.Rows.Add()

            If i > 0 Then
                DataGridView1.Rows(i).Cells(0).Value = (i + 1) + Val(DataGridView1.Rows(i - 1).Cells(0).Value)
            Else
                DataGridView1.Rows(i).Cells(0).Value = i + 1
            End If

        Next

End Sub

Hope it can give you your desired result.

thank for everybody enthusiastic support
wish everybody a Happy New Year

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.