Click here to Skip to main content
15,914,222 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 836.5K   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

 
Generalusing this control Pin
smokie_30-Aug-07 22:59
smokie_30-Aug-07 22:59 
QuestionHow about an Expand/Collapse all function Pin
vonhaupt1-Aug-07 7:52
vonhaupt1-Aug-07 7:52 
AnswerRe: How about an Expand/Collapse all function Pin
Andrei Bozantan1-Aug-07 22:53
Andrei Bozantan1-Aug-07 22:53 
GeneralRe: How about an Expand/Collapse all function Pin
vonhaupt2-Aug-07 1:45
vonhaupt2-Aug-07 1:45 
QuestionThe expand button doesnt work Pin
tineo24-Jul-07 8:56
tineo24-Jul-07 8:56 
AnswerRe: The expand button doesnt work Pin
Camille9831-Jul-07 3:01
Camille9831-Jul-07 3:01 
AnswerRe: The expand button doesnt work Pin
JL. ROBERT19-Sep-07 22:47
JL. ROBERT19-Sep-07 22:47 
GeneralExpansion and column headers in extgridview Pin
Camille9820-Jun-07 6:06
Camille9820-Jun-07 6:06 
Hello. First, thanks for developing this and posting it for everyone. It is exactly what we needed. I was able to get it running and populated in the manner that i need for my company (using datasets). The problem that I am having is that upon expanding the childgrids (I have two), the data and header in the parent row shift to the right, I'm assuming to take into account the colspan of the newly expanded childgrid. In the demo that is posted, it is not noticeable with the customer grid because there are enough columns to accommodate this. But, if you reduced the number of columns displayed in the very first grid to at most 3, you can see that the columns do in fact shift when clicking the expand button, and then shift back to the left when clicking the collapse button (I have all of my headertext columns aligned to the right, which is our standard style for column data). The same occurs with the orders grid. If I expand an order row to show orderdetails, the columns in the order row also shift. In addition, if I omit the headertext from the boundfield object, no shifting occurs at all. So, I'm assuming that it has to do with the headers, but I am at a loss of how to fix this.

So, the scenario is this: on page load, the page only shows the rows for the customer grid. Everything looks fine and is aligned to the left (data and header text). I expand one of the customer rows to see order data. The order grid displays, but every customer row shifts to the right so that everything is aligned center of the order grid. The text in the order grid is now aligned to the left. I expand one of the rows in the order grid, and the order details grid appears. Now, the data and header text in all of the order rows have shifted to the right so that they are now centered above the order detail grid. The order details grid text appears left aligned as it should. Collapsing a row returns the text for the rows in the corresponding grid to their proper left alignment.

Does anyone have any ideas?
GeneralRe: Expansion and column headers in extgridview Pin
Camille9831-Jul-07 3:06
Camille9831-Jul-07 3:06 
Questionhi Pin
raffu15-Jun-07 21:34
raffu15-Jun-07 21:34 
QuestionEdit mode does not work Pin
JL. ROBERT14-Jun-07 7:39
JL. ROBERT14-Jun-07 7:39 
AnswerRe: Edit mode does not work Pin
JL. ROBERT17-Jun-07 23:54
JL. ROBERT17-Jun-07 23:54 
GeneralRe: Edit mode does not work Pin
wyanshan13-Apr-09 3:35
wyanshan13-Apr-09 3:35 
Question2.x version Pin
mjcusimano@hotmail.com12-Jun-07 9:26
mjcusimano@hotmail.com12-Jun-07 9:26 
AnswerRe: 2.x version Pin
mjcusimano@hotmail.com12-Jun-07 9:29
mjcusimano@hotmail.com12-Jun-07 9:29 
Generalevents in nested grids Pin
pete999999994-Jun-07 0:44
pete999999994-Jun-07 0:44 
QuestionBadly need help in ExtGridView(Template gridview) Pin
mekhaleed3-Jun-07 16:52
mekhaleed3-Jun-07 16:52 
QuestionTestExtGridView is incomplete Pin
Olivertech25-May-07 9:58
Olivertech25-May-07 9:58 
GeneralBind the detail grid when click Pin
Member 14232798-Mar-07 23:01
Member 14232798-Mar-07 23:01 
GeneralCannot run demo project Pin
quinnie7-Mar-07 8:06
quinnie7-Mar-07 8:06 
Generalsorting Pin
mblyakher26-Feb-07 6:43
mblyakher26-Feb-07 6:43 
QuestionRe: sorting Pin
mblyakher6-Mar-07 12:45
mblyakher6-Mar-07 12:45 
QuestionJavascript error - "Unspecified Error" Pin
M71720-Feb-07 8:38
M71720-Feb-07 8:38 
AnswerRe: Javascript error - &quot;Unspecified Error&quot; Pin
shane husband20-Feb-07 10:23
shane husband20-Feb-07 10:23 
QuestionRe: Javascript error - &quot;Unspecified Error&quot; Pin
M71720-Feb-07 11:01
M71720-Feb-07 11:01 

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.