Hi,
I am a new here and found the article of "Mudassar Ahmed Khan about Nested GridView Example in ASP.Net using C# and VB.Net
I tried it with a third level, but have problems of binding data. Does anyone know, how it could work?
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" CssClass="Grid"
DataKeyNames="t1000_customer_id" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor: pointer" src="./images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid"
DataKeyNames="t1001_order_id" OnRowDataBound="OnRowDataBound2">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor: pointer" src="./images/plus.png" />
<asp:Panel ID="pnlInfos" runat="server" Style="display: none">
<asp:GridView ID="gvInfos" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid2"
DataKeyNames="t1002_t1001_id_pk" OnRowDataBound="OnRowDataBound2">
<Columns>
<asp:BoundField ItemStyle-Width="300px" DataField="t1002_info" HeaderText="Info" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="t1001_order_id" HeaderText="OrderId" />
<asp:BoundField ItemStyle-Width="150px" DataField="t1001_date" HeaderText="Date" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="t1000_customer_name" HeaderText="Name" />
<asp:BoundField ItemStyle-Width="150px" DataField="t1000_customer_region" HeaderText="Region" />
</Columns>
</asp:GridView>
In .cs File a added level 2 databound:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
string sql = string.Format("select t1001_order_id, t1001_date from t1001_orders where t1001_t1000_id_pk={0}", customerId);
gvOrders.DataSource = GetData(sql);
gvOrders.DataBind();
}
}
For Level 3 I tried:
protected void OnRowDataBound2(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvOrders = e.Row.FindControl("gvOrders") as GridView;
string orderId = gvOrders.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvInfos = gvOrders.FindControl("gvInfos") as GridView;
string sql = string.Format("select t1002_t1001_id_pk, t1002_info from t1002_order_infos where t1002_t1001_id_pk={0}", orderId);
gvInfos.DataSource = GetData(sql);
gvInfos.DataBind();
}
}
Best regards Hipp