5,401,186 members and growing! (18,274 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate License: The Code Project Open License (CPOL)

Grid Inside a Grid - Nested Grid in C#

By Gigy

Grid Inside a Grid Control on a web page
C# (C# 1.0, C# 2.0, C#), Javascript, CSS, HTML, .NET (.NET, .NET 1.1, .NET 2.0), ASP, ASP.NET, Ajax

Posted: 15 Mar 2008
Updated: 15 Mar 2008
Views: 8,369
Bookmarked: 14 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.83 Rating: 3.35 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2
0 votes, 0.0%
3
3 votes, 42.9%
4
2 votes, 28.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This code snippet will help to create a Nested Grid. The sub grid depends on the each row element in the parent grid control

Background

I had a requirement to load all acounts of an enrolled user in a grid control for a financial project. Some of the accounts have sub accounts which has to be loaded/ shown as a sub elements to that account. Even though several sites gives an idea to create the sub grids, but none of them was prefect match.

Using the code

The code contains to simple grid controls which has been used in a tricky way to show like a sub grid.

This is the code

            <table>
    <tr>
     <td>
      <asp:DataGrid id="ctlGrdLoanAccount" runat="server" AutoGenerateColumns="False" 
    BorderColor="#CC9966"
       BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4">
       <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
       <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66">
    </SelectedItemStyle>
       <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
       <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000">
    </HeaderStyle>
       <Columns>
        <asp:BoundColumn DataField="Account" HeaderText="Accounts">
         <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
         <ItemStyle HorizontalAlign="Left" Width="120px"></ItemStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="InterestYTD" HeaderText="InterestYTD" 
    DataFormatString="{0:F2}%">
         <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
         <ItemStyle HorizontalAlign="Left" Width="120px"></ItemStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="MaturityDate" HeaderText="MaturityDate" 
    DataFormatString="{0:d}">
         <HeaderStyle HorizontalAlign="Left" Width="100px"></HeaderStyle>
         <ItemStyle HorizontalAlign="Left"></ItemStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="PaymentAmount" HeaderText="PaymentAmount" 
    DataFormatString="{0:C2}">
         <HeaderStyle HorizontalAlign="Left" Width="100px"></HeaderStyle>
         <ItemStyle HorizontalAlign="Left"></ItemStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="CurrentBalance" HeaderText="CurrentBalance" 
    DataFormatString="{0:C2}">
         <HeaderStyle HorizontalAlign="Left" Width="100px"></HeaderStyle>
         <ItemStyle HorizontalAlign="Left"></ItemStyle>
        </asp:BoundColumn>
        <asp:TemplateColumn>
         <ItemTemplate>
     </td>
    </tr>
    <tr>
     <td colspan="6" align="center">
      <asp:datagrid id="ctlGrdSubAccount" Visible="false" runat="server" 
       EnableViewState="False" AutoGenerateColumns="False"
       BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" 
       CellPadding="4">
       <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
       <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66">
    </SelectedItemStyle>
       <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
       <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000">
    </HeaderStyle>
       <Columns>
        <asp:BoundColumn DataField="SubAccount" HeaderText="SubAccount">
         <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
         <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="Balance" HeaderText="Balance" 
    DataFormatString="{0:C2}">
         <ItemStyle HorizontalAlign="Left" Width="50px"></ItemStyle>
         <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="APR" HeaderText="APR" DataFormatString="{0:F2}%">
         <ItemStyle HorizontalAlign="Left" Width="50px"></ItemStyle>
         <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
        </asp:BoundColumn>
        <asp:BoundColumn DataField="PayOffAmount" HeaderText="PayOffAmount" 
    DataFormatString="{0:C2}">
         <ItemStyle HorizontalAlign="Left" Width="100px"></ItemStyle>
         <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
        </asp:BoundColumn>
       </Columns>
      </asp:datagrid>
      </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid></td>
    </tr>
   </table>




 private void ctlGrdLoanAccount_ItemDataBound(object sender, 
    System.Web.UI.WebControls.DataGridItemEventArgs args)
  {
   if (args.Item != null) 
   { 
    object [] rowArray = new object[3];
    DataGrid g = (DataGrid) sender;
    DataTable table = g.DataSource as DataTable;
    if (args.Item.ItemIndex != -1) 
    {
     DataRow datarow = table.Rows[args.Item.ItemIndex];
     rowArray = datarow.ItemArray;
     //Check for the sub Accounts 
     if (dsAccount.Tables.Contains("33333")) 
     {
      ctlGrdSubAccount  = (DataGrid)args.Item.FindControl("ctlGrdSubAccount")  
    as DataGrid;
      ctlGrdSubAccount.Visible = true;
      ctlGrdSubAccount.DataSource = dsAccount.Tables["33333"];
      ctlGrdSubAccount.DataBind();
     }
    }
   }
  
  }




  private void ctlGrdLoanAccount_ItemCreated(object sender, 
    System.Web.UI.WebControls.DataGridItemEventArgs args)
  {
   args.Item.Cells[4].Style.Add("BORDER-RIGHT-STYLE", "none");   
   args.Item.Cells[5].Style.Add("BORDER-TOP-STYLE", "none");
  // args.Item.Cells[5].Style.Add("BORDER-RIGHT-STYLE", "none");
   args.Item.Cells[5].Style.Add("BORDER-LEFT-STYLE", "none");
   
  }

Histroy

Intital Draft : 15-03-2009

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gigy


Gigy works as a consultant. He holds a Masters in Information Technology and has over nine years of experience in various critical technology domains.

He has hands on experience in the development and maintenance of projects related to Healthcare Domain, Image Processing, Banking, Simulation and Porting in C, C++, Visual C++ and DotNet.

He also works in COM, ATL, C#, VB.NET, ASP.NET,ADO.NET, SSRS

www.gigyonline.com
Occupation: Software Developer (Senior)
Company: NCR
Location: United States United States

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralNice TrickmemberSeaWater8:29 14 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Mar 2008
Editor:
Copyright 2008 by Gigy
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project