Controls Hierarchy in DataGrid

K

Kubuli John

It appears that there are only 2 ways to get to a particular control
in a DataGrid - either use the FindControl method or try to work your
way through the (apparently undocumented) hierarchy of controls
(dgItem.Controls[0].Controls[0].Controls[2]...).

Using FindControl is OK except that to make generic sort buttons in
the headers of template columns, I have to add LinkButtons. If I use
FindControl to get to these controls (to enable or disable them when
switching between view and edit modes), they each have to have a
unique name. This means that I have to hard-code the control id's
into my code, making it difficult to reuse the code when I change what
the grid is displaying. Ditto for the images since I have to hide all
other images when I show one.

I was hoping to be able to iterate through all of the controls in the
header row looking for LinkButtons called "btnSort", but I do not want
to try to decipher the hierarchy of controls to use
dgItem.Controls[0].Controls[1]... It doesn't look like there is a
single Controls collection that goes through all of the control
hierarchy either.

The Columns collection is no use either since it only gives me access
to the generic header style items, not my custom header controls.

Is there a documented hierarchy that I can rely on to locate the
appropriate control, or some other way to navigate through a DataGrid
or DataGridItem?

Thanks,

John H.
 
R

Rick Spiewak

Here's what I would recommend:
In the ItemCreated event:

Dim dg As DataGrid = DirectCast(sender, DataGrid)
Dim itm As DataGridItem = e.Item
Select Case itm.ItemType
..
..
..
Case ListItemType.Header
For i As Integer = 0 To itm.Cells(0).Controls.Count - 1
Dim c As Control = itm.Cells(0).Controls(i)

If c.GetType.ToString =
"System.Web.UI.WebControls.DataGridLinkButton" Then
dglb = DirectCast(c, LinkButton)

' Your code here
..
..
..
Case Else
Exit Sub
End Select
End Sub

For i As Integer = 0 To itm.Cells(0).Controls.Count - 1
Dim c As Control = itm.Cells(0).Controls(i)

If c.GetType.ToString =
"System.Web.UI.WebControls.DataGridLinkButton" Then
dglb = DirectCast(c, LinkButton)
 
J

John Haasbeek

Thanks for the reply. I was trying to add the little up and down sort
indicators in the headers and eded up just naming the images imgSortUp0,
imgSortUp2, etc. and imgSortDn0, imgSortDn1, ...

Then I was able to use the FindControl method to turn all of the images
on or off - I just had to remember to change the training number in each
column header when I pasted the images in.

John H.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top