Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

referencing 6th column of DataGrid in ASP.NET Web Forms

In this example from this post

https://www.experts-exchange.com/questions/29119837/clearing-a-Header-Row-CheckBox-in-a-DataGrid.html

My DataGrid looked like this:

User generated image
Notice my CheckBox column is the 1st column of the DataGrid

My button click event looked like this, this worked fine:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' reset header checkbox
        Dim dgHeader As DataGridItem = DGEmployees.Controls(0).Controls(0)
        Dim targetChkBox As CheckBox = dgHeader.Controls(0).Controls(1)
        targetChkBox.Checked = False
        CType(DGEmployees.Controls(0).Controls(0).FindControl("DataGridEmployees_SelectAll"), CheckBox).Checked = False

    End Sub

Open in new window



In these 2 lines of code:

        Dim dgHeader As DataGridItem = DGEmployees.Controls(0).Controls(0)
        Dim targetChkBox As CheckBox = dgHeader.Controls(0).Controls(1)

What are these references?

.Controls(0).Controls(0)
.Controls(0).Controls(1)

Is that the first row and 1st column?

So if my datagrid looked like this (the checkboxcolumn is the 6th column of my datagrid),

User generated image
How do I reference the 6th column of my datagrid?

I tried this but it didn't work,
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' reset header checkbox
        Dim dgHeader As DataGridItem = DGEmployees.Controls(0).Controls(0)
        Dim targetChkBox As CheckBox = dgHeader.Controls(0).Controls(6)
        targetChkBox.Checked = False
        CType(DGEmployees.Controls(0).Controls(0).FindControl("DataGridEmployees_SelectAll"), CheckBox).Checked = False

    End Sub

Open in new window


I get this error message:

Specified argument was out of the range of valid values.
Parameter name: index
ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial