Gridview within another gridview


Gridview within another gridview

This exmaple show how we can nest gridview within 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 n 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="25" Width="900px"
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound"
meta:resourcekey="GridView1Resource1" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Employee Loan Balance Details">
<ItemTemplate>
<table>
<tr>
<td>
Company</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"></asp:Label></td>
</tr>
<tr>
<td colspan="6">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" EnableTheming="False"
PageSize="20" Width="100%" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowDataBound="GridView1_RowDataBound" meta:resourcekey="GridView1Resource1"
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>



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++;
}
}
}


Related Articles

What is GridView control and how to bind GridView to data?

What is a GridView control in asp.net? Want to learn how to connect GridView to a database? This article will teach you more about the databound control GridView, properties and events of GridView and binding a GridView control to data.

How to add Buttons in Data GridView?

Want to add buttons to the GridView control in Asp.net? Need to execute a set of statements when the user clicks on the button in the GridView? Learn how to add controls like button to the GridView control and write C# code for user activities performed like Clicking of Buttons in the GridView.

More articles: Gridview within Gridview Gridview nesting Gridview in Gridview Gridview

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: