Click here to Skip to main content
Click here to Skip to main content

ASP.NET Expand/Contract DataGrid

By , 14 Apr 2004
 
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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGetting an error: javascript function ToggleExpand not foundmemberMember 834247023 May '12 - 20:08 
Hi..
 
I tried executing the code with the dll rebuit with the error fix mentioned to use the ClientID. Still I get an error saying that the javascript function :Toggle expand is undefined.. Frown | :(
 
The observation I could make is that the dynamic file created had the script (having toggleExpand function) just above the dynamically created table.. It is not under the <head> tag. Confused | :confused:
 
Can anyone please post a fix for this.
 
Thanks,
Sruthi
QuestionExdatagrid in Ajax update panel - Regmembersrivinayl5 Aug '11 - 19:36 
It is good one, whether we can use this Grid in Ajax update panel
GeneralGood OnememberAnurag Gandhi27 Oct '10 - 23:10 
hi,
This is very nice. Got my 5.
Please have a look over my similar article: <a target="_blank" href="http://www.codeproject.com/KB/webforms/GroupGridView.aspx">Group GridView Data</a>
Anurag Gandhi.
http://www.gandhisoft.com
Life is a computer program and every one is the programmer of his own life.
My latest article: Group GridView Data

GeneralPlsss help me with the aspx.cs codememberMember 447242625 Apr '10 - 7:00 
I have created a table named streptomyces in SQL server.. This table contains the columns isolates,refrence , species , antibiotics , resistant.
Now i need to create a page in asp.net in which there is a text box search. When we enter the search value , we need to search the entire table if that word is present.
If that particular search word is found , I need to display all the columns for that particular row.
Plss help me how to create the aspx.cs file. Plss its really urgent..
Mansi
 
Student

Questionreg exporting to excelmembercsivakumar13 Jun '06 - 18:31 
I am trying the expand/contract concept in a datagrid in vs2003.
when i try the same grid for exporting to excel i cant export it into excel.
can anyone please help me in this case.
 

the code i used is:
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
Response.Charset = String.Empty;
StringWriter myTextWriter = new StringWriter();
HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myTextWriter);
Exdatagrid1.RenderControl(myHtmlTextWriter);
Response.Write(myTextWriter.ToString());
 
siva.
AnswerRe: reg exporting to excelmembereric.lacourse13 Jun '06 - 20:05 
The below excerpt works for me when outputting office docs-though I am usually dealing with a buffer.   I would imagine you could replace the Response.OutputStream.Write part with your TextWriter.
 
// Clear the response buffer
Response.Clear();
 
// add a header so we can name our file [Optional]
string strFileName = "your_file.xls";
Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName);
 
// Set the content-type so the browser knows what to do
Response.ContentType = "application/vnd.ms-excel";
 
// now write out the file data..yours is textual so use what you have above i guess
// Response.OutputStream.Write((byte[])buffer, 0, intLength);
 
// End your response stream
Response.End();
 
HTH,
 
lacourem
GeneralRe: reg exporting to excelmembercsivakumar14 Jun '06 - 23:34 
hi eric,
Thanks for your reply.
i got the exported excel file.
But the gif image is there in the exported excel file.
i cleared the special controls like link.
can anyone help me in this case to clear the gif image.
GeneralCollapse/Expand iconmemberRom200012 Jan '06 - 0:33 
Help!Frown | :(
 
is it possible to add icon in collapse/expand column header? to toggle the expand/collapse of all rows.
 
Thanks.
 
-- modified at 6:33 Thursday 12th January, 2006
GeneralBest Gridmemberalmagunet16 Jun '05 - 4:53 
Congratulations, quite nice grid
QuestionAlternatingItemStyle property?sussmcclar22 Oct '04 - 2:56 
Tony - this is a really great grid - I've been coding ASP.NET for about a year now, but am only starting to get into creating my own controls, and looking at yours has helped immensely... my question is about adding an AlternatingItemStyle property to the ExColumn class. When I try to use the property, I get the following error:
 
Type 'Gorth.ExDataGrids.ExColumn' does not have a property named 'AlternatingItemStyle'.
 
Could you help me learn how to add this to your exColumn.cs file? Thanks a million!
 
Shelley

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

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