VB.NET - Accessing the RowDataBound when the gridview is in another gridview

Asked By Annie X on 29-Nov-11 04:53 AM
Hi,
I have a gridview in a gridview. Usually I would access the RowDataBound as below

Protected Sub gvTravelOptions_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvTravelOptions.RowDataBound


but as the gridview (gvTravelOptions) is inside the other gridview (gvTravelCards).

I get the following error msg
Error 2 Handles clause requires a WithEvents variable defined in the containing type or one of its base types. 

How would I access the RowDataBound for gvTravelOptions. Im quite new to vb.net and not sure what to type.

Thanks Annie X

<asp:GridView ID="gvTravelCards"
       runat="server"
       Width="100%"
       AutoGenerateColumns="False" 
       BorderStyle="None"
       BorderColor="#ffffff"
       BorderWidth="0px"       
       CellPadding="4"
       CellSpacing="0"
       ShowHeader="False"
       ShowFooter="False">
        <Columns>
       
         <asp:TemplateField>
            <ItemTemplate>
              <asp:Label ID="lblTitle" runat="server" CssClass="NCT-H9-BK-B"></asp:Label><br />
              <asp:Label ID="lblInfo" runat="server" CssClass="NCT-H9-BK"></asp:Label><br />
                                     
             
              <%--START: GridView in a GridView--%>
              <asp:GridView ID="gvTravelOptions"
               runat="server"
               Width="100%"
               AutoGenerateColumns="False" 
               BorderStyle="None"
               BorderColor="#ffffff"
               BorderWidth="0px"       
               CellPadding="4"
               CellSpacing="0"
               ShowHeader="True"
               ShowFooter="False">
                <Columns>       
               
                
                 <asp:TemplateField>                   
                    <ItemTemplate>
                      <asp:Label ID="lblProductName" runat="server" CssClass="NCT-H9-BK"></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" VerticalAlign="Bottom" BorderStyle="None" CssClass="NCT-H9-BK-B" />
                    <ItemStyle HorizontalAlign="Left" Width="40%" BorderStyle="None" />     
                  </asp:TemplateField>
                 
                  <asp:TemplateField HeaderText="<u>Price</u>">
                    <ItemTemplate>
                      <asp:Label ID="lblPrice" runat="server" CssClass="NCT-H9-BK"></asp:Label>           
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Left" VerticalAlign="Bottom" BorderStyle="None" CssClass="NCT-H9-BK-B" />
                    <ItemStyle HorizontalAlign="Left" Width="10%" BorderStyle="None" />     
                  </asp:TemplateField>
                 
                  <asp:TemplateField>
                    <ItemTemplate>
                      <asp:Button ID="btnSelectTravelOption" CommandName="btnSelectTravelOption" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>' runat="server" CssClass="NCT-H9-BK-B"  />
                    </ItemTemplate>    
                  </asp:TemplateField>          
                </Columns>       
              </asp:GridView>
              <%--START: GridView in a GridView--%>
             

             
            </ItemTemplate>            
          </asp:TemplateField>        
        </Columns>       
      </asp:GridView>

Reena Jain replied to Annie X on 29-Nov-11 04:58 AM
hi,

You have to bind the data for the second grid in the RowDataBound event of the first grid like
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType.Equals(DataControlRowType.DataRow))
  {
    //Since you said you have a grid view inside a gridview.
    GridView gridView2 = e.Row.FindControl("GridView2") as GridView;
    //Assuming GetData() will return the datasource
    gridView2.DataSource = GetData();
    //When DataBind is called, the RowDataBound event for
    //gridview2 will be fired
    gridView2.DataBind();
  }
}
Jitendra Faye replied to Annie X on 29-Nov-11 04:59 AM
Design your Grid like this-


<

asp:GridView ID="GridView1" runat="server"

onrowdatabound="GridView1_RowDataBound">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView2_RowDataBound">

<Columns>

<asp:TemplateField>

<ItemTemplate>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>



Now in .cs file

//Parent Grid

protected

void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

}

//Child Grid


protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)

{

}



Try this and let em know.
Annie X replied to Reena Jain on 29-Nov-11 05:02 AM
Thank you for the help