Click here to Skip to main content
15,884,472 members
Articles / Web Development / ASP.NET
Article

ExtGridView

Rate me:
Please Sign up or sign in to vote.
4.79/5 (68 votes)
5 Dec 2005CPOL2 min read 829.9K   13.6K   214   180
A control which displays hierarchical data using the ASP.NET DataGrid or GridView control.

ExtGridView in action!

Introduction

This article presents a control which displays hierarchical data using the ASP.NET GridView control. This extension of the GridView control can also be used to display extended data details for a data row.

Using the code

The ExtGridView control can be used just like the regular ASP.NET GridView control (since it's inherited from GridView). The ExtGridView control will take care to displaying the expand/collapse button and also shows or hides the nested content.

But how should the nested content be added to the grid? The idea is simple. Because each row can have some related nested content, the ExtGridView control assumes that this content is placed in the last column of the grid. So the content from the last column will not be displayed as a regular column in the grid, but rather as the content which is displayed when the expand button is clicked.

In the sample project, the Customers table from the Northwind database is displayed using the ExtGridView control. The customer's orders are displayed in another ExtGridView as nested content. Finally, the Order details are displayed as nested content for the rows in the Orders grid. If it's necessary, when running the sample project, please modify the connection string in web.config.

Please notice that instead of the grid from the last column, you can place any other ASP.NET control (or HTML code) inside the ItemTemplate and the control will be displayed as nested content.

Points of Interest

Customizing the expand/collapse button's appearance

The ExtGridView control defines the following properties that can be used to customize the expand/collapse button appearance:

  • ExpandButtonCssClass - specifies the CSS class name that is used for the expand button.
  • CollapseButtonCssClass - specifies the CSS class name that is used for the collapse button.
  • ExpandButtonText - specifies the text for the expand button (default value is "+").
  • CollapseButtonText - specifies the text for the collapse button (default value is "-").

Note that you can specify HTML code as values for the ExpandButtonText and the CollapseButtonText properties. (This can be used as a workaround for the 'anchors with the background image flickering' bug in IE.)

HTML
<cc:ExtGridView runat="server" id="grid1" 
  ExpandButtonText="<img src='Images/expand.gif' alt='+' border='0' />" 
  CollapseButtonText="<img src='Images/collapse.gif' alt='-' border='0' />" ...

Hiding the expand/collapse button

Each row in the ExtGridView is actually an instance of the ExtGridViewRow class. So any object of type GridViewRow that is a row in an ExtGridView control can be converted to ExtGridViewRow. So, when you want to hide the expand/collapse button for a row in the ExtGridViewRow control, first convert the GridRow object to ExtGridViewRow and then you can use the ShowExpand property to specify if the expand/collapse button is visible or not.

C#
ExtGridViewRow row = grid.Rows[0] as ExtGridViewRow;
row.ShowExpand = false;

History

  • 2005-11-21 - First version release.
  • 2005-11-24 - Updated the expand/collapse button's behavior.
  • 2005-11-28 - Added the ExtDataGrid control which is supported by .NET 1.x.
  • 2005-12-05 - Added a sample project for using the ExtDataGrid control (.NET 1.1). Image 2

License

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


Written By
Software Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Database Efficiency Pin
Jacatak2-Oct-06 18:29
Jacatak2-Oct-06 18:29 
GeneralMultiple ObjectDataSource Pin
Drudo29-Sep-06 4:16
Drudo29-Sep-06 4:16 
QuestionLevel Pin
kmatth00728-Sep-06 6:03
kmatth00728-Sep-06 6:03 
Questionhelp - how can I do Expand/Collapse all? Pin
Tong Chen @ Seattle19-Sep-06 7:50
Tong Chen @ Seattle19-Sep-06 7:50 
AnswerRe: help - how can I do Expand/Collapse all? Pin
Andrei Bozantan19-Sep-06 8:24
Andrei Bozantan19-Sep-06 8:24 
GeneralRe: help - how can I do Expand/Collapse all? Pin
Tong Chen @ Seattle19-Sep-06 8:40
Tong Chen @ Seattle19-Sep-06 8:40 
GeneralRe: help - how can I do Expand/Collapse all? Pin
Tong Chen @ Seattle19-Sep-06 8:58
Tong Chen @ Seattle19-Sep-06 8:58 
GeneralRe: help - how can I do Expand/Collapse all? Pin
Andrei Bozantan19-Sep-06 9:32
Andrei Bozantan19-Sep-06 9:32 
You should call that function when you want to expand the grid.
Samples:
1. in the aspx page

<a href="#" onclick="expand(document.getElementById('<%= grid1.ClientID %>'), true);">Expand the first grid</a>

2. in the code behind
btn.Attributes["onclick"] = "expand(document.getElementById('" + grid1.ClientID + "'), true);";

If you have two nested ExtGridView's (I don't understand exactly your case) it's a little more complicated to expand the inner one, and you can do it only from the code behind.

Regards,
Andrei
GeneralRe: help - how can I do Expand/Collapse all? Pin
Tong Chen @ Seattle19-Sep-06 10:45
Tong Chen @ Seattle19-Sep-06 10:45 
GeneralRe: help - how can I do Expand/Collapse all? Pin
Martin Aathanya30-Apr-08 3:58
Martin Aathanya30-Apr-08 3:58 
Generaladd GridView [modified] Pin
Phan Thanh Lam18-Sep-06 15:40
Phan Thanh Lam18-Sep-06 15:40 
GeneralRe: add GridView Pin
Tong Chen @ Seattle19-Sep-06 7:54
Tong Chen @ Seattle19-Sep-06 7:54 
GeneralChange database Pin
thesemplice14-Sep-06 9:58
thesemplice14-Sep-06 9:58 
QuestionProblem with the child gridview Pin
Jasondev13-Sep-06 0:03
Jasondev13-Sep-06 0:03 
QuestionPager style Pin
Sandeep Singhvi8-Sep-06 1:59
Sandeep Singhvi8-Sep-06 1:59 
AnswerRe: Pager style Pin
Andrei Bozantan9-Sep-06 8:22
Andrei Bozantan9-Sep-06 8:22 
GeneralPlease Help me in Paging. Pin
Member 13566037-Sep-06 22:50
Member 13566037-Sep-06 22:50 
GeneralRe: Please Help me in Paging. Pin
Andrei Bozantan9-Sep-06 8:27
Andrei Bozantan9-Sep-06 8:27 
GeneralPaging is Not Working Pin
Member 13566034-Sep-06 21:18
Member 13566034-Sep-06 21:18 
GeneralRe: Paging is Not Working Pin
Andrei Bozantan9-Sep-06 8:36
Andrei Bozantan9-Sep-06 8:36 
GeneralBreaks Footer Pin
dude_401229-Aug-06 18:50
dude_401229-Aug-06 18:50 
GeneralRe: Breaks Footer Pin
Andrei Bozantan9-Sep-06 8:54
Andrei Bozantan9-Sep-06 8:54 
GeneralRe: Breaks Footer Pin
dude_401211-Sep-06 16:40
dude_401211-Sep-06 16:40 
QuestionNu intra in evenimentul RowCommand Pin
Liviu Ignat29-Aug-06 2:45
Liviu Ignat29-Aug-06 2:45 
AnswerRe: Nu intra in evenimentul RowCommand Pin
Andrei Bozantan9-Sep-06 9:03
Andrei Bozantan9-Sep-06 9:03 

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

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