Friday, May 17, 2013

Gridview inside gridview || Nested gridview in Asp.net

Title:How to use grid view inside grid view in asp.net using c#.net

Description:
Earlier we have seen how to Export grid view data to PDF,Bind data to grid view in asp.net.Here i will given an example for how to develop the nested grid view in asp.net.In this example the binding has done while page load event then the child grid view will populate on row data bound event.
Example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Gridview inside gridview or Nested GridView in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GvParentOrderReport" runat="server" OnRowDataBound="GvOrderReport_RowDataBound"
AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="OrderId"
GridLines="Horizontal">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Orderid" HeaderText="OrderId">
</asp:BoundField>
<asp:BoundField DataField="OrderName" HeaderText="OrderName" >
</asp:BoundField>
<asp:BoundField DataField="Phone" HeaderText="Phone" >
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address" >
</asp:BoundField>
<asp:BoundField DataField="Amount" HeaderText="Amount" >
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td>
<asp:GridView ID="GvChildOrderNested" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="OrderId" HeaderText="OrderID"/>
<asp:BoundField DataField="OrderName" HeaderText="OrderName"/>
<asp:BoundField DataField="Phone" HeaderText="Phone"/>
<asp:BoundField DataField="Address" HeaderText="Address"/>
<asp:BoundField DataField="Amount" HeaderText="Amount"/>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>

using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _GridviewInsideGridview : System.Web.UI.Page
{
SqlConnection mcon = new SqlConnection("Data Source=Bhaskar\sqlexpress;Initial Catalog=Test;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
mcon.Open();
SqlCommand Parentcmd = new SqlCommand("select * from orders", mcon);
SqlDataAdapter Parentda = new SqlDataAdapter(Parentcmd);
DataSet Parentds = new DataSet();
Parentda.Fill(Parentds);
mcon.Close();
GvParentOrderReport.DataSource = Parentds;
GvParentOrderReport.DataBind();
}
}
     
protected void GvOrderReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
mcon.Open();
GridView GvChildOrderNested = (GridView)e.Row.FindControl("GvChildOrderNested");
SqlCommand Childcmd = new SqlCommand("select * from orders", mcon);
SqlDataAdapter Childda = new SqlDataAdapter(Childcmd);
DataSet Childds = new DataSet();
Childda.Fill(Childds);
mcon.Close();
GvChildOrderNested.DataSource = Childds;
GvChildOrderNested.DataBind();
}
}
}
Post a Comment

Bel