dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersMore...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET GridView

Gridview within another gridview


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 8


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 Resources:


Read related articles: Gridview within Gridview    Gridview nesting    Gridview in Gridview    Gridview    


Did you like this resource? Share it with your friends and show your love!


Responses to "Gridview within another gridview"

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

Feedbacks      

Post 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Merge two dataset
    Previous Resource: Auto Generated Serial Number In Gridview Control
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Gridview within another gridview asp.net 2.0  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.