mercredi 31 août 2016

If checkbox Checked then add to datatable from Gridview

I am trying check whether a checkbox is checked in a gridview and if it is checked to add it to the datatable.

However I am getting an error when the checkbox is unchecked for the row:

There is no row at position 1.

Here is my code:

       'Creates a new datatable
        Dim dtQuestions As New DataTable("QuestionsData")

        'Add columns to datatable
        For Each cell As TableCell In example.HeaderRow.Cells

            dtQuestions.Columns.Add(cell.Text)

        Next

        For Each row As GridViewRow In example.Rows

            Dim chkTest As CheckBox = CType(row.FindControl("chkTest"), CheckBox)
            If chkTest.Checked = True Then

                dtQuestions.Rows.Add()

                For i As Integer = 0 To row.Cells.Count - 1

                    Try

                        dtQuestions.Rows(row.RowIndex)(i) = row.Cells(i).Text

                    Catch ex As Exception

                    End Try


                Next

            Else

                'Do not add it to Datatable

            End If

        Next

I am getting the error on this code:

dtQuestions.Rows(row.RowIndex)(i) = row.Cells(i).Text

I do not know how to fix this.




Aucun commentaire:

Enregistrer un commentaire