Click here to Skip to main content
15,885,278 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 830K   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

 
Questionhow to create facebook like post and comment box in asp.net Pin
safal.78614-Aug-14 9:06
safal.78614-Aug-14 9:06 
GeneralMy vote of 5 Pin
Justin Clarke28-Jan-11 3:43
Justin Clarke28-Jan-11 3:43 
Excellent solution. Really fast on the client-side as well.
GeneralExpand All / Collapse All Feature [modified] Pin
Justin Clarke28-Jan-11 2:05
Justin Clarke28-Jan-11 2:05 
Generalexpand grid row to show usercontrol page when a row is expanded Pin
rocky8111-Sep-10 5:06
rocky8111-Sep-10 5:06 
GeneralError: tbl.rows is undefined Pin
bertvaes29-Dec-09 8:14
bertvaes29-Dec-09 8:14 
GeneralRe: Error: tbl.rows is undefined Pin
bertvaes29-Dec-09 21:48
bertvaes29-Dec-09 21:48 
QuestionAnyway to preform a postback when clicking the expand button? Pin
joekelly26-Dec-09 6:24
joekelly26-Dec-09 6:24 
AnswerRe: Anyway to preform a postback when clicking the expand button? Pin
Andrei Bozantan27-Dec-09 4:34
Andrei Bozantan27-Dec-09 4:34 
GeneralRe: Anyway to preform a postback when clicking the expand button? [modified] Pin
joekelly27-Dec-09 10:15
joekelly27-Dec-09 10:15 
QuestionNeed one more level down Pin
sunny3520-Jul-09 17:37
sunny3520-Jul-09 17:37 
AnswerRe: Need one more level down Pin
Andrei Bozantan21-Jul-09 0:11
Andrei Bozantan21-Jul-09 0:11 
GeneralRe: Need one more level down Pin
sunny3521-Jul-09 14:38
sunny3521-Jul-09 14:38 
Generalerror binding data to a label in second level grid.. Pin
ranisagar7-Jul-09 4:59
ranisagar7-Jul-09 4:59 
GeneralEvents in the child grid are not firing Pin
ranisagar5-Jul-09 21:24
ranisagar5-Jul-09 21:24 
GeneralRe: Events in the child grid are not firing Pin
Andrei Bozantan5-Jul-09 22:57
Andrei Bozantan5-Jul-09 22:57 
GeneralRe: Events in the child grid are not firing Pin
ranisagar7-Jul-09 4:13
ranisagar7-Jul-09 4:13 
GeneralRe: Events in the child grid are not firing Pin
ramh0819-Jul-09 20:45
ramh0819-Jul-09 20:45 
GeneralRe: Events in the child grid are not firing Pin
Andrei Bozantan19-Jul-09 22:47
Andrei Bozantan19-Jul-09 22:47 
GeneralGridView Doesn't retain it's Data After PostBack.. Pin
chudhary.faisal1-Jul-09 5:08
chudhary.faisal1-Jul-09 5:08 
GeneralBig Problem! Add Edit/select command in ExtGridView not working!!! Pin
wyanshan8-Apr-09 19:01
wyanshan8-Apr-09 19:01 
Generalfisrt level grid for first row is missing when clicking on expand/collapse all Pin
vidyas19826-Feb-09 2:24
vidyas19826-Feb-09 2:24 
Questionexpand/collapse button hide Pin
irfanMalvi67835-Dec-08 21:53
irfanMalvi67835-Dec-08 21:53 
Generalexpand/collapse button hide Pin
irfanMalvi67835-Dec-08 18:01
irfanMalvi67835-Dec-08 18:01 
GeneralSorting the grid(critical requirement and help will be appriciated Pin
vidyas198224-Nov-08 4:15
vidyas198224-Nov-08 4:15 
GeneralRe: Sorting the grid(critical requirement and help will be appriciated Pin
Andrei Bozantan24-Nov-08 4:55
Andrei Bozantan24-Nov-08 4:55 

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.