5,317,180 members and growing! (24,088 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Intermediate License: The Code Project Open License (CPOL)

Expanding / Collapsing GridView Rows

By SAMir Nigam

This article describes how to expand and collapse rows of a GridView using JavaScript.
C# (C# 2.0, C#), Javascript, CSS, XHTML, HTML, .NET (.NET, .NET 2.0), ASP.NET, ADO.NET, Ajax, Dev

Posted: 7 May 2008
Updated: 15 May 2008
Views: 13,523
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
37 votes for this Article.
Popularity: 7.15 Rating: 4.56 out of 5
0 votes, 0.0%
1
1 vote, 2.7%
2
0 votes, 0.0%
3
8 votes, 21.6%
4
28 votes, 75.7%
5

preview.gif

Introduction

This article presents expand/collapse functionality for GridView rows using JavaScript. To demonstrate this functionality, I’ve used an Image on the GridView header. Rows of the GridView will expand and collapse on successive clicks on this Image.

Using the HTML code

Add a TemplateField inside the GridView and put an Image in the HeaderTemplate of the TemplateField. The HTML code for the GridView will look like this:

<asp:GridView ID="gvTab" BackColor="WhiteSmoke" runat="server" 
     AutoGenerateColumns="False" GridLines="Vertical" ShowFooter="True">
   <Columns>
      <asp:TemplateField>
         <HeaderStyle Width="25px" />
         <ItemStyle Width="25px" BackColor="White" />
         <HeaderTemplate>
            <asp:Image ID="imgTab" onclick="javascript:Toggle(this);" 
                 runat="server" ImageUrl="~/minus.gif" ToolTip="Collapse" />
         </HeaderTemplate>
      </asp:TemplateField>
      <asp:BoundField HeaderText="n" DataField="n">
         <HeaderStyle Width="25px" />
         <ItemStyle Width="25px" />
      </asp:BoundField>
      <asp:BoundField HeaderText="sqrt(n)" DataField="sqrtn">
         <HeaderStyle Width="150px" />
         <ItemStyle Width="150px" />
      </asp:BoundField>
      <asp:BoundField HeaderText="qbrt(n)" DataField="qbrtn">
         <HeaderStyle Width="150px" />
         <ItemStyle Width="150px" />
      </asp:BoundField>
   </Columns>
   <HeaderStyle Height="25px" Font-Bold="True" BackColor="DimGray" 
                ForeColor="White" HorizontalAlign="Center"
                VerticalAlign="Middle" />
   <RowStyle Height="25px" BackColor="Gainsboro" 
             HorizontalAlign="Center" VerticalAlign="Middle" />
   <AlternatingRowStyle Height="25px" BackColor="LightGray" 
             HorizontalAlign="Center" VerticalAlign="Middle" />
   <FooterStyle BackColor="Gray" />
</asp:GridView>

Attach an onclick event to the GridView header’s Image.

<asp:Image ID="imgTab" onclick="javascript:Toggle(this);" runat="server" 
           ImageUrl="~/minus.gif" ToolTip="Collapse" />

Using the JavaScript code

Put the following code in the script tag:

<script type="text/javascript">
 var Grid = null;
 var UpperBound = 0;
 var LowerBound = 1;
 var CollapseImage = 'minus.gif';
 var ExpandImage = 'plus.gif';
 var IsExpanded = true;   
 var Rows = null;
 var n = 1;
 var TimeSpan = 25;
        
 window.onload = function()
 {
    Grid = document.getElementById('<%= this.gvTab.ClientID %>');
    UpperBound = parseInt('<%= this.gvTab.Rows.Count %>');
    Rows = Grid.getElementsByTagName('tr');
 }
        
 function Toggle(Image)
 {
    ToggleImage(Image);
    ToggleRows();  
 }    
        
 function ToggleImage(Image)
 {
    if(IsExpanded)
    {
       Image.src = ExpandImage;
       Image.title = 'Expand';
       Grid.rules = 'none';
       n = LowerBound;
             
       IsExpanded = false;
    }
    else
    {
       Image.src = CollapseImage;
       Image.title = 'Collapse';
       Grid.rules = 'cols';
       n = UpperBound;
                
       IsExpanded = true;
    }
 }
        
 function ToggleRows()
 {
    if (n < LowerBound || n > UpperBound)  return;
                            
    Rows[n].style.display = Rows[n].style.display == '' ? 'none' : '';
    if(IsExpanded) n--; else n++;
    setTimeout("ToggleRows()",TimeSpan);
 }
</script>

In the above code, global variables have been initialized in the window.onload event. There are three methods: Toggle, ToggleImage, and ToggleImage. The Toggle method is invoked on successive clicks on the header Image. This method first toggles the header image and then toggles the rows of the GridView by invoking the ToggleImage and ToggleImage methods. Note that the ToggleRows method is invoked here recursively, using the setTimeout method. I’ve invoked it recursively here in order to create a dynamic effect during the expanding/collapsing of the GridView rows.

In order to make a delay in each recursion, each call to the ToggleRows method has been delayed for 25 milliseconds. You can change it by altering the TimeSpan value depending on your needs.

Conclusion

In this article, I've used the setTimeout method in order to achieve a smooth expand/collapse functionality.

I have tested this script on the following browsers:

Browsers.png

License

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

About the Author

SAMir Nigam


Currently I'm working as a Sr. Software Engineer in SRM Techsol Pvt. Ltd., Lucknow, INDIA. I possess following degrees:

  • MCA from U.P. Technical University, Lucknow, INDIA.
  • PGDCA from Institute of Engineering and Rural Technology, Allahabad, INDIA.
  • BSc from University of Allahabad, Allahabad, INDIA.


Occupation: Software Developer (Senior)
Company: STPL
Location: India India

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 25 of 45 (Total in Forum: 45) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralHow we make this grid nestedmemberRehan Hussain2:22 13 Jun '08  
GeneralCollapsing the rowsmemberMember 435482922:03 11 Jun '08  
GeneralRe: Collapsing the rowsmember SAMir Nigam 22:15 11 Jun '08  
QuestionGridLines getting washed out on first expand !!!!memberPrash1:34 28 May '08  
AnswerRe: GridLines getting washed out on first expand !!!!member SAMir Nigam 19:43 28 May '08  
GeneralNice OnememberSatya Kanithi7:00 19 May '08  
GeneralRe: Nice Onemember SAMir Nigam 19:05 19 May '08  
GeneralNice and simple implementationmemberAntony M Kancidrowski1:35 15 May '08  
GeneralRe: Nice and simple implementationmember SAMir Nigam 3:06 15 May '08  
Generalthanxxxxxmembersnopbear4:44 13 May '08  
GeneralRe: thanxxxxxmember SAMir Nigam 18:45 13 May '08  
Generalstatus after postbackmemberpetr.snobelt1:02 13 May '08  
GeneralRe: status after postbackmember SAMir Nigam 18:59 13 May '08  
QuestionCollapse/Expand panelsmemberS_Aijaz10:18 13 May '08  
AnswerRe: Collapse/Expand panelsmember Samir Nigam 0:41 13 May '08  
GeneralRe: Collapse/Expand panelsmemberS_Aijaz10:45 13 May '08  
GeneralRe: Collapse/Expand panelsmember Samir Nigam 0:58 13 May '08  
QuestionRe: Collapse/Expand panelsmemberS_Aijaz11:13 13 May '08  
AnswerRe: Collapse/Expand panelsmember SAMir Nigam 19:08 13 May '08  
QuestionRe: Collapse/Expand panelsmemberS_Aijaz121:45 13 May '08  
AnswerRe: Collapse/Expand panelsmember SAMir Nigam 19:34 14 May '08  
GeneralRe: Collapse/Expand panelsmemberS_Aijaz13:17 6 Jun '08  
GeneralRe: Collapse/Expand panelsmemberS_Aijaz13:19 6 Jun '08  
GeneralRe: Collapse/Expand panelsmember SAMir Nigam 20:36 6 Jun '08  
GeneralIts really goodmemberIslam ElDemery23:44 12 May '08  

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

PermaLink | Privacy | Terms of Use
Last Updated: 15 May 2008
Editor: Smitha Vijayan
Copyright 2008 by SAMir Nigam
Everything else Copyright © CodeProject, 1999-2008
Web13 |