Click here to Skip to main content
6,594,932 members and growing! (16,100 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Intermediate License: The Code Project Open License (CPOL)

Grid Inside a Grid - Nested Grids in C#

By Gigy

Nested grid controls on a web page.
C# (C# 1.0, C# 2.0), Javascript, CSS, HTML, .NET (.NET 1.1, .NET 2.0), ASP, ASP.NET, Ajax
Posted:15 Mar 2008
Views:23,161
Bookmarked:28 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 3.63 Rating: 3.48 out of 5
2 votes, 18.2%
1
1 vote, 9.1%
2

3
4 votes, 36.4%
4
4 votes, 36.4%
5

Introduction

This code snippet will help create a nested grid. A sub-grid depends on each row element in a parent grid control.

Background

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

Using the code

The code contains two simple grid controls which have been used in a tricky way as to shown one as a sub grid.

Here is the ASPX 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>

Here is the C# code:

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");
}

History

  • Initial draft: 15-Mar-2008.

License

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

About the Author

Gigy


Member
Gigy is a Software professional from India, now living in United States of America. He is having over 9 years of experience in various phases of software development. Worked in the capacity of Project Leader, Senior Software Engineer, Senior Programmer and Programmer in the development and maintenance of applications & products related to Online Banking, Insurance Brokerage, Image Processing, Healthcare Domain, Simulation, Graphics, Automation, Add - Ins and Porting.

He has worked in versatile technolgies viz. C, C++, MFC, Visual C++, COM, DCOM, ATL, Windows Services and Visual Studio IDE. Currently working in .Net Technologies - AJAX, C#, Visual Basic.NET, Managed C++, ASP.NET, SOAP, XML, DTD, XSL, Web Services and Visual Studio. NET 2005, 2008 IDEs.

He is presently working with NCR in Internet Banking Technologies. He has also worked with world reputed companies like Accenture, General Electric and Mphasis.

He is now focusing in WPF, Silverlight, WCF, Azure, MMC and Velocity Technologies

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

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralHow to handle button click event of my Child gridview Pinmemberkish2020:31 14 Sep '09  
GeneralMy vote of 1 Pinmemberdivyeshhearty3:14 17 Mar '09  
GeneralLooking for Windows.Forms.DataGridView or old DataGird with this feature Pinmembervkuttyp3:35 9 Oct '08  
GeneralNice Trick PinmemberSeaWater8:29 14 Aug '08  
GeneralRe: Nice Trick PinmemberGigy6:39 2 Oct '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: Smitha Vijayan
Copyright 2008 by Gigy
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project