Hi guys can you help me in this one? I can add cell values in Datagrid using their column address or column name but I want to add the columns which contain a specific word for example I want to add all cells with a column name containing "Amount"

        Dim rw = 0
        Dim ax = Form1.DataGridView1.RowCount - 1
        Dim cl = 1
        Dim dy = Form1.DataGridView1.ColumnCount


                If Form1.DataGridView1.Columns(cl).Name.Contains("Amount") = True Then

                    Form1.DataGridView1.Rows(rw).Cells("Final Computation").Value += Form1.DataGridView1.Rows(rw).Cells(cl).Value

                    cl = cl + 1


                    cl = cl + 1

                End If

            Loop While (cl < dy)

            rw = rw + 1
        Loop While (rw < ax)

It says Index is out of range.

Recommended Answers

All 3 Replies

Check if there is a column "Final Computation". Could be your culprit.

I have checked it. The error shows at line 9

Problem solved.............

 Dim t As Integer
        For i As Integer = 0 To Form1.DataGridView1.Rows.Count - 2

            For j As Integer = 0 To Form1.DataGridView1.Columns.Count - 1

                If Form1.DataGridView1.Columns(j).Name.Contains("Amount") = True Then

                    t += Form1.DataGridView1.Rows(i).Cells(j).Value

                End If

            Form1.DataGridView1.Rows(i).Cells("Final Computation").Value = t
            t = 0
Be a part of the DaniWeb community

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