Nested GridView in asp.net using C#
Gridview within another gridview
This exmaple show how we can nest gridview.GridView inside other gridview for disply related data from database.Gridview can be nested in other Gridview control to display the related data of any item retrieved from database using C# code.
Steps:
1. Drag & drop a gridview in to the page.
2. Add a new template column to this gridview.
3. Place another gridview inside this template column.
<asp:GridView ID="GridView2" runat="server" EnableTheming="False" PageSize="15" Width="700px"
OnPageIndexChanging="GridView2_PageIndexChanging" OnRowDataBound="GridView2_RowDataBound"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Employee Details">
<ItemTemplate>
<table>
<tr>
<td>Company Name </td>
<td>:</td>
<td colspan="4"><asp:Label ID="lblCompany" runat="server" Text='<%# Bind("Company") %>'></asp:Label></td>
</tr>
<tr>
<td>Date</td>
<td>:</td>
<td colspan="4"><asp:Label ID="lblDate" runat="server">
</tr>
<tr>
<td colspan="6">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" EnableTheming="False"
PageSize="10" Width="100%" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="User Name" HeaderText="Name">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="LoanName" HeaderText="Loan">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Show Name" HeaderText="Payroll Month">
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="PDate" HeaderText="Paid Date">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="OpeningBalance" HeaderText="Open Date">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="PaidAmount" HeaderText="Amt. Paid">
<ItemStyle HorizontalAlign="Right" >
</asp:BoundField>
</Columns>
<RowStyle CssClass="reporttext" />
<EmptyDataTemplate>
<strong><span style="font-size: 10pt;">No Records....!</span> </strong>
</EmptyDataTemplate>
<PagerStyle CssClass="reportheader" />
<HeaderStyle CssClass="reportheader" Width="80px" />
<AlternatingRowStyle CssClass="reporttext" />
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs code
protected void FirstGridBind()
{
DataTable dt1DataTable = new DataTable();
dt1.Columns.Add("CompanyName");
DataRow drDataRow = dt1DataTable.NewRow();
if (ddlCompany.SelectedValue != "0")
{
drDataRow["CompanyName"] = ddlCompany.SelectedItem.Text;
}
else
{
drDataRow["CompanyName"] = "All Companies";
}
dt1DataTable.Rows.Add(drDataRow);
GridView2.DataSource = dt1DataTable;
GridView2.DataBind();
}
protected void SecondGridBind()
{
foreach (GridViewRow row in GridView2.Rows)
{
Label lblDate = row.FindControl("lblDate") as Label;
if (txtFromDate.Text.Trim() != "" && txtToDate.Text.Trim() != "")
{
lblDate.Text = txtFromDate.Text.Trim() + " - " + txtToDate.Text.Trim();
}
else
{
lblDate.Text = "All Dates";
}
GridView GridView1 = row.FindControl("GridView1") as GridView;
string str = @"SELECT You query;
if (ddlCompany.SelectedValue != "0")
{
str += " AND (hrdEmployeeMaster.CompanyId = " + Convert.ToInt32(ddlCompany.SelectedValue) + ")";
}
DataTable dt = SqlHelper.ExecuteDataset(connString, CommandType.Text, str).Tables[0];
Session["myDataTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
if (GridView1.Rows.Count > 0)
{
lbExport.Visible = true;
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (GridView2.Rows.Count > 0)
{
GridView GridView1 = GridView2.Rows[0].FindControl("GridView1") as GridView;
e.Row.Cells[0].Text = Convert.ToString(GridView1.PageIndex * GridView1.PageSize + SrNo);
SrNo++;
}
}
}
Hi santosh,
ReplyDeleteReally your code help me,but can you tell me how you do paging on both grid(i.e parent and child).
Thanks
Great!!!!!! this is a good one,
ReplyDeletefor more... details you can check following links...
http://www.mindstick.com/Articles/2552ee5c-add6-435d-87ae-57abf42b23be/?GridView%20inside%20GridView%20in%20ASP.Net%20%28Nested%20GridView%29
Use some kind of code formatting for your blog. This is annoying to look at.
ReplyDeleteC# GridView for .NET applications
ReplyDeleteNested grids exemplify efficient data representation. They allow hierarchical display of related information within a parent grid, enhancing readability and reducing clutter. What Military Encryption This design fosters organized user experiences, making complex data structures more accessible and understandable in a visually coherent manner.
ReplyDelete