Click here to Skip to main content
Licence 
First Posted 2 Feb 2004
Views 167,012
Downloads 4,343
Bookmarked 103 times

ASP.NET Expand/Contract DataGrid

By | 14 Apr 2004 | Article
ASP.NET Expand/Contract DataGrid that allows you to show/hide details without posting back.
Prize winner in Competition "ASP.NET Jan 2004"

Introduction

The ASP.NET DataGrid is very powerful and robust. However, some of the things we need it to do require lots of custom code. Denis Bauer's HierarGrid is an example of a very sophisticated datagrid. Unfortunately, my project needed the ability to expand and contract DataGrid columns rather than DataGrid rows, so I implemented the ExDataGrid server control which is described in this article. Much of the code logic was borrowed from HierarGrid.

You can check out the HierarGrid here.

You can also check out a live demo of ExDataGrid here.

Figure 1. Contracted Columns

Figure 2. Expanded Columns

Using the server control

The ExDataGrid server control requires the use of three classes:

  • ExDataGrid - Main server control. This is directly derived from DataGrid and has all functionality of a regular datagrid.
  • ExColumnExpand - Column with a plus and minus image. This control is derived from BoundColumn.
  • ExColumn - Columns that will contract/expand. This control is derived from BoundColumn and has added properties such as DrawBorders, ForeColor, and BackColor.

To associate the ExColumnExpand and ExColumn classes so that they work together, the property ExGroupName must be assigned for each expansion group.

Example:

<cc1:ExDataGrid id="DataGridReport1" runat="server" 
             BorderColor="#000066" CellPadding="1" BorderWidth="2px"
    AutoGenerateColumns="False" ShowFooter="True">
    <EditItemStyle ForeColor="Black" BackColor="Yellow"></EditItemStyle>
    <AlternatingItemStyle Font-Size="X-Small" BackColor="Gainsboro">
    </AlternatingItemStyle>
    <ItemStyle Font-Size="X-Small" Font-Names="Verdana"></ItemStyle>
    <HeaderStyle Font-Bold="True" BackColor="#D7D7D7"></HeaderStyle>
    <FooterStyle Font-Size="Small" BackColor="RoyalBlue"></FooterStyle>
    <Columns>
        <asp:BoundColumn DataField="Title_ID" HeaderText="Title_ID" 
                         SortExpression="Title_ID"></asp:BoundColumn>
        <cc1:ExColumnExpand ExGroupName="TitleExpand"  DataField="Title" 
                         HeaderText="Title" SortExpression="Title" />
        <cc1:ExColumn DataField="Price" ExGroupName="TitleExpand" 
                         HeaderText="Price" SortExpression="Price"
            DrawBorders="true">
            <ItemStyle BackColor="#396794" ForeColor="white"></ItemStyle>
            <HeaderStyle BackColor="#396794" ForeColor="white"></HeaderStyle>
            <FooterStyle BackColor="#396794" ForeColor="white"></FooterStyle>
        </cc1:ExColumn>
        <cc1:ExColumn DataField="Notes"  ExGroupName="TitleExpand" 
                         HeaderText="Notes" SortExpression="Notes" 
             DrawBorders="true">
            <ItemStyle BackColor="#396794" ForeColor="white"></ItemStyle>
            <HeaderStyle BackColor="#396794" ForeColor="white"></HeaderStyle>
            <FooterStyle BackColor="#396794" ForeColor="white"></FooterStyle>
        </cc1: ExColumn>
        <asp:BoundColumn DataField="Type" 
               HeaderText="Type" SortExpression="Type" />
        <cc1:ExColumnExpand DataField="Publisher" 
                   ExGroupName="PublishExpand" HeaderText="Publisher" 
             SortExpression="Publisher" />
        <cc1:ExColumn DataField="Publication"  ExGroupName="PublishExpand" 
                   HeaderText="Publication Date" 
            SortExpression="Publication" DrawBorders="false">
            <ItemStyle BackColor="#396794" ForeColor="white"></ItemStyle>
        </cc1:ExColumn>
        <asp:BoundColumn DataField="Authors" HeaderText="Authors" 
                   SortExpression="Authors"></asp:BoundColumn>
    </Columns>
</cc1: ExDataGrid>

Conclusion

This server control was quickly written and has much room for improvement. It does implement the IPostback interface so the state of the control is saved during a sort or any other rebinding of data. Additionally, the control's expand and contract functions run via JavaScript so a postback is not necessary. Please make any suggestions and or improvements as necessary. Let me know how it goes!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tony Truong

Architect
Frontline Direct Inc., Adconion
United States United States

Member

Tony Truong graduated from UCLA in Spring of 2001 and starting worked at Symantec Corporation as a Software Engineer. After a few years of developing various features for Norton SystemWorks, Tony moved to San Diego. He is currently writing database applications using ASP.NET and C# with the .NET Framework. Tony specializes in tara-byte databases with emphasis on high availability, optimization, and complex entity modeling.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGetting an error: javascript function ToggleExpand not found PinmemberMember 834247020:08 23 May '12  
QuestionExdatagrid in Ajax update panel - Reg Pinmembersrivinayl19:36 5 Aug '11  
GeneralGood One PinmemberAnurag Gandhi23:10 27 Oct '10  
GeneralPlsss help me with the aspx.cs code PinmemberMember 44724267:00 25 Apr '10  
Questionreg exporting to excel Pinmembercsivakumar18:31 13 Jun '06  
AnswerRe: reg exporting to excel Pinmembereric.lacourse20:05 13 Jun '06  
GeneralRe: reg exporting to excel Pinmembercsivakumar23:34 14 Jun '06  
GeneralCollapse/Expand icon PinmemberRom20000:33 12 Jan '06  
GeneralBest Grid Pinmemberalmagunet4:53 16 Jun '05  
QuestionAlternatingItemStyle property? Pinsussmcclar2:56 22 Oct '04  
GeneralFormatting data in columns PinsussAnonymous22:58 12 Sep '04  
GeneralRe: Formatting data in columns PinsussAnonymous0:46 14 Sep '04  
GeneralConfusing Error PinmemberImhoteph3:42 12 Jun '04  
GeneralRe: Confusing Error PinmemberTony Truong12:50 17 Jun '04  
QuestionOnly on IE? PinsussAnonymous11:05 27 May '04  
GeneralEditing within the expanded grid Pinmembermick_lennon4:37 19 May '04  
GeneralRe: Editing within the expanded grid Pinmemberkraftspl0:14 21 May '04  
GeneralRe: Editing within the expanded grid Pinmembermick_lennon22:48 21 May '04  
GeneralOnline test Pinmemberkraftspl11:20 6 May '04  
GeneralRe: Online test PinmemberTony Truong11:32 6 May '04  
QuestionRe: Online test PinmemberLouisa Chen18:36 18 Jun '07  
GeneralFounded a little bug... Pinmemberataru2:48 24 Feb '04  
GeneralRe: Founded a little bug... PinmemberTony Truong12:09 13 Apr '04  
GeneralRe: Founded a little bug... Pinmembersinjiy7:55 21 Apr '04  
Generalexpand row PinmemberSubandi Wahyudi17:25 18 Feb '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 15 Apr 2004
Article Copyright 2004 by Tony Truong
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid