Guido Gabriel
Guido Gabriel

Reputation: 23

GridView not found in CodeBehind ASP NET

I have a page with a gridview1 with only one column of ItemTemplate. Inside this ItemTemplate in my gridview1 i have a gridview2. I have no problem with the gridview1. In codebehind i got populate it normally:

gridview1.DataSource = DaoUser.findAll();
gridview1.DataBind();

And the gridview1 is created correctly. But when i try doing the same thing with gridview2, i ve got a message error:

gridview2.DataSource = DaoUser.findAll();
gridview2.DataBind();

The name 'gridview2' does not exist in the current context

My aspx code:

 <asp:GridView runat="server" ID="gridview1">
    <colums>
        <asp:TemplateField>
            <ItemTemplate>
                     <asp:GridView runat="server" ID="gridview2">
                     </asp:Gridview>
            </ItemTemplate>
        </asp:TemplateField>
    </columns>    
 </asp:GridView>

Upvotes: 1

Views: 727

Answers (1)

Steve
Steve

Reputation: 549

What you have to do is locate the control in the row using the GridView1_RowDataBound event since you will have a grid view in each row (this is in VB):

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound

    If e.Row.RowState = DataControlRowState.Normal And e.Row.RowType = DataControlRowType.DataRow Then

        Dim tmpGridView As GridView = e.Row.FindControl("GridView2")

        If Not tmpGridView Is Nothing Then
            tmpGridView.DataSource = DaoUser.findAll
            tmpGridView.DataBind()
        End If

    End If


End Sub

Upvotes: 3

Related Questions