finding HyperlinkColumn in DataGrid from behind code

lounge56

Newcomer
Joined
Feb 5, 2004
Messages
14
Hello! I am a little bit confused with how to access a specific HyperlinkColumn in my data grid. I loop through each item in my datagrid items, but cannot seem to figure out the appropiatesyntax to reach this bound column. I need to get to it from the behind code because I will modify certain properties from there.

'************ behind code sample *******************

For Each ite In Datagrid1.Items

'this is how i find and modify my controls in the template column in the data
'grid without a problem
imgbutton = CType(ite.FindControl("imgbutSurvDelete"), ImageButton)
imgbutton.Visible = True


'this is how i am trying to find the hyperlikcolumn in the datagrid.
'what i mean by ite.Controls(2) is the second childcontrol (or bound column)
'within the datagrid, but it doesn't work b/c it is not at same hierarchy level
'as HyperLinkColumn).
If arrOptionName(x) = "View" Then
hyplinkcolumn = CType(ite.Controls(2), HyperLinkColumn)

Next

'************** web form code sample *******************

<asp:datagrid id="griGenSurvey" BorderWidth="0" HeaderStyle-VerticalAlign="Top" Width="900px" cellpadding="2" cellspacing="0" runat="server" AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#cccccc" ItemStyle-VerticalAlign="Top" HeaderStyle-HorizontalAlign="Center">
<Columns>
<asp:BoundColumn DataField="SurveyID" HeaderText="#" HeaderStyle-CssClass="000000Text12Bold" ItemStyle-CssClass="000000Text11Bold"></asp:BoundColumn>

<asp:BoundColumn DataField="SurveyName" HeaderText="Name" HeaderStyle-CssClass="000000Text12Bold" ItemStyle-CssClass="000000Text11Normal"></asp:BoundColumn>

<!-- I assume this is (2) in data grid chil;d control index -->
<asp:HyperinkColumn DataField="SurveyTitle" HeaderText="Title" HeaderStyle-CssClass="000000Text12Bold" ItemStyle-CssClass="000000Text11Normal"></asp:HyperlinkColumn>

<asp:TemplateColumn HeaderText="Controls" ItemStyle-Width="85" HeaderStyle-CssClass="000000Text12Bold" ItemStyle-CssClass="000000Text11Normal">
<ItemTemplate>
</asp:hyperlink>
<asp:ImageButton id="imgbutSurvDelete" ImageUrl="../../images/icon_trashcan.gif" runat="server" CommandName="DeleteEvent" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "SurveyID") %>' OnCommand="imgbutSurvDelete_Command" Visible="False">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


**************************************************

Any help would be appreciated.

Thank you!

-ricardo 8~)
 
Have you tried this..

Code:
        Dim item As DataGridItem
        Dim cell As DataGridColumn
        Dim strType As String
        For Each item In dgrd.Items
            For Each cell In item.Cells
                strType = cell.GetType.ToString
            Next
        Next
 
navigate each cell in each datgrid item

Thank you for answering kahlua001! I tried your suggestion, but when I set:

Dim cell As datagrid.Cells

...cell is always = Nothing

It is as if I can't see the cells as i loop through each item (row).

Therefore, when the following is executed:

For Each cell In ite.Cells
strType = cell.GetType.ToString 'note: error in this line
Next

...I get "System.InvalidCastException: Specified cast is not valid"
 
Back
Top