Click here to Skip to main content
15,908,112 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 835.6K   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

 
QuestionUnknown server tag 'cc:ExtGridView'. Pin
Oddisj28-Feb-07 22:12
Oddisj28-Feb-07 22:12 
Questiondynamic hierarchy ? Pin
shiftbit8-Dec-06 3:33
shiftbit8-Dec-06 3:33 
GeneralEditing Grids [modified] Pin
srinivas7g30-Nov-06 12:15
srinivas7g30-Nov-06 12:15 
QuestionHow to expand all records when page starts? Pin
tvtanh20-Nov-06 20:54
tvtanh20-Nov-06 20:54 
QuestionHow to allow for columns that are not visible? Pin
gnwrg20-Nov-06 11:15
gnwrg20-Nov-06 11:15 
GeneralExtGridView Pin
hutty19-Nov-06 17:02
hutty19-Nov-06 17:02 
AnswerRe: ExtGridView - TestGridControl.ascx.vb Pin
peters252120-Nov-06 6:05
peters252120-Nov-06 6:05 
AnswerRe: ExtGridView: ExtGridView.vb Pin
peters252120-Nov-06 6:09
peters252120-Nov-06 6:09 
'ExtGridView.vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.ComponentModel



Namespace CustomControls
'''
''' ExtGridViewRow extendes the standard GridView row to render the contents from the last cell as an expandible cell
'''

Public Class ExtGridViewRow
Inherits GridViewRow
Private _expCell As TableCell
Private _ihExp As HtmlInputHidden
Private _ctlExpand As HtmlAnchor
Private _showExpand As Boolean

'''
''' Gets or sets a value which specifies if the expand boutton should be displayed or not for the current row.
'''

Public Property ShowExpand() As Boolean
Get
Return _showExpand
End Get
Set(ByVal value As Boolean)
_showExpand = Value
End Set
End Property

'''
''' Constructor for ExtGridViewRow
'''

''' <param name="rowIndex" />
''' <param name="dataItemIndex" />
''' <param name="rowType" />
''' <param name="rowState" />
Public Sub New(ByVal rowIndex As Integer, ByVal dataItemIndex As Integer, ByVal rowType As DataControlRowType, ByVal rowState As DataControlRowState)
MyBase.New(rowIndex, dataItemIndex, rowType, rowState)
End Sub


'''
''' Overrides GridViewRow.OnInit to perform custom initialization of the row.
'''

''' <param name="e" />event args
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)

If RowType = DataControlRowType.Header Then
_expCell = New TableHeaderCell()
ElseIf RowType = DataControlRowType.DataRow Then
_expCell = New TableCell()

_ctlExpand = New HtmlAnchor()
_ctlExpand.HRef = "#"
_ctlExpand.Attributes("onclick") = "TglRow(this);"

_ihExp = New HtmlInputHidden()
_ihExp.ID = "e" & Me.DataItemIndex.ToString()

_expCell.Controls.Add(_ctlExpand)
_expCell.Controls.Add(_ihExp)
End If


If Not _expCell Is Nothing Then
_expCell.Width = Unit.Pixel(20)

Cells.AddAt(0, _expCell)
End If
End Sub


'''
''' Overrides GridViewRow.Render to perform custom rendering of the row.
'''

''' <param name="writer" />the HtmlTextWrite object in which the row is rendered



Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
If DesignMode Then
MyBase.Render(writer)
Return
End If

Dim c As TableCell = Cells(Cells.Count - 1)

If RowType = DataControlRowType.DataRow Then
If _showExpand Then
Dim grid As ExtGridView = TryCast(Me.Parent.Parent, ExtGridView)

If _ihExp.Value = String.Empty Then
_ctlExpand.InnerHtml = grid.ExpandButtonText
_ctlExpand.Attributes("class") = grid.ExpandButtonCssClass
Else
_ctlExpand.InnerHtml = grid.CollapseButtonText
_ctlExpand.Attributes("class") = grid.CollapseButtonCssClass
End If

c.Visible = False
MyBase.Render(writer)

c.Visible = True
c.ColumnSpan = GetVisibleCellsCount() - 1
c.BackColor = BackColor

If _ihExp.Value = String.Empty Then
writer.Write("")
Else
writer.Write("")
End If

c.RenderControl(writer)

writer.Write("")


If RowIndex = grid.Rows.Count - 1 Then
If (grid.BottomPagerRow Is Nothing AndAlso grid.FooterRow Is Nothing) OrElse ((Not grid.BottomPagerRow Is Nothing AndAlso grid.BottomPagerRow.Visible = False) AndAlso Not grid.FooterRow Is Nothing AndAlso grid.FooterRow.Visible = False) Then
writer.Write("")
End If
End If
Else
_ihExp.Visible = False
_ctlExpand.Visible = _ihExp.Visible
c.Visible = False
MyBase.Render(writer)
End If

ElseIf RowType = DataControlRowType.Header Then
c.Visible = False
MyBase.Render(writer)
Else
MyBase.Render(writer)
End If
End Sub


'''
''' Helper method which obtains the visible cells count in the current row.
'''

''' <returns>
Private Function GetVisibleCellsCount() As Int32
Dim ret As Int32 = 0

For Each c As TableCell In Cells
If c.Visible Then
ret += 1
End If
Next c

Return ret
End Function
End Class


'''
''' ExtGridView implements the expandible GridView behaviour.
'''

Public Class ExtGridView
Inherits GridView
Private _expandButtonCssClass As String
Private _collapseButtonCssClass As String
Private _expandButtonText As String = "+"
Private _collapseButtonText As String = "-"

'''
''' Sets or gets the CSS class which is applied on the expand button control.
'''

<category("styles")> _
Public Property ExpandButtonCssClass() As String
Get
Return _expandButtonCssClass
End Get
Set(ByVal value As String)
_expandButtonCssClass = value
End Set
End Property


'''
''' Sets or gets the CSS class which is applied on the collapse button control.
'''

<category("styles")> _
Public Property CollapseButtonCssClass() As String
Get
Return _collapseButtonCssClass
End Get
Set(ByVal value As String)
_collapseButtonCssClass = value
End Set
End Property

'''
''' Sets or gets the expand button's text.
'''

<category("appearance")> _
Public Property ExpandButtonText() As String
Get
Return _expandButtonText
End Get
Set(ByVal value As String)
_expandButtonText = value
End Set
End Property

'''
''' Sets or gets the collapse button's text.
'''

<category("appearance")> _
Public Property CollapseButtonText() As String
Get
Return _collapseButtonText
End Get
Set(ByVal value As String)
_collapseButtonText = value
End Set
End Property


'''
''' Overrides GridView.OnInit to perform custom initialization of the control.
'''

''' <param name="e" />
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)

'#Region "register script"
Dim script As String = "function TglRow(ctl)" & ControlChars.CrLf & "{" & ControlChars.CrLf & " var row = ctl.parentNode.parentNode;" & ControlChars.CrLf & " var tbl = row.parentNode;" & ControlChars.CrLf & " var crow = tbl.rows[row.rowIndex + 1];" & ControlChars.CrLf & " var ihExp = ctl.parentNode.getElementsByTagName('input').item(0);" & ControlChars.CrLf & ControlChars.CrLf & " tbl = tbl.parentNode;" & ControlChars.CrLf & ControlChars.CrLf & " var expandClass = tbl.attributes.getNamedItem('expandClass').value;" & ControlChars.CrLf & " var collapseClass = tbl.attributes.getNamedItem('collapseClass').value;" & ControlChars.CrLf & " var expandText = tbl.attributes.getNamedItem('expandText').value;" & ControlChars.CrLf & " var collapseText = tbl.attributes.getNamedItem('collapseText').value;" & ControlChars.CrLf & ControlChars.CrLf & " " & ControlChars.CrLf & " if (crow.style.display == 'none')" & ControlChars.CrLf & " {" & ControlChars.CrLf & " crow.style.display = '';" & ControlChars.CrLf & " ctl.innerHTML = collapseText;" & ControlChars.CrLf & " ctl.className = collapseClass;" & ControlChars.CrLf & " ihExp.value = '1';" & ControlChars.CrLf & " }" & ControlChars.CrLf & " else" & ControlChars.CrLf & " {" & ControlChars.CrLf & " crow.style.display = 'none';" & ControlChars.CrLf & " ctl.innerHTML = expandText;" & ControlChars.CrLf & " ctl.className = expandClass;" & ControlChars.CrLf & " ihExp.value = '';" & ControlChars.CrLf & " }" & ControlChars.CrLf & "}"

Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "ExtGrid", script, True)
'#End Region

End Sub

'''
''' Overrides GridView.CreateRow to create the custom rows in the grid (ExtGridViewRow).
'''

''' <param name="rowIndex" />
''' <param name="dataSourceIndex" />
''' <param name="rowType" />
''' <param name="rowState" />
''' <returns>
Protected Overrides Function CreateRow(ByVal rowIndex As Integer, ByVal dataSourceIndex As Integer, ByVal rowType As DataControlRowType, ByVal rowState As DataControlRowState) As GridViewRow
Return New ExtGridViewRow(rowIndex, dataSourceIndex, rowType, rowState)
End Function


Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Me.Attributes("expandClass") = _expandButtonCssClass
Me.Attributes("collapseClass") = _collapseButtonCssClass
Me.Attributes("expandText") = _expandButtonText
Me.Attributes("collapseText") = _collapseButtonText

MyBase.Render(writer)
End Sub

End Class

End Namespace


Public Class ExtGridView

End Class
GeneralRe: ExtGridView Pin
hutty21-Nov-06 15:23
hutty21-Nov-06 15:23 
GeneralRe: ExtGridView Pin
vikramkalsan16-Jan-07 1:48
vikramkalsan16-Jan-07 1:48 
GeneralRe: ExtGridView Pin
mjohnen17-Jun-07 11:14
mjohnen17-Jun-07 11:14 
QuestionHow to export Extgridview to Excel? Pin
peters252118-Nov-06 8:07
peters252118-Nov-06 8:07 
QuestionRe: How to export Extgridview to Excel? 2nd Pin
peters252120-Dec-06 11:38
peters252120-Dec-06 11:38 
QuestionCollapse all Pin
darin vanatta5-Nov-06 15:19
darin vanatta5-Nov-06 15:19 
GeneralCollapse all Pin
darin vanatta5-Nov-06 15:18
darin vanatta5-Nov-06 15:18 
QuestionAdjusting Column width Pin
peters252119-Oct-06 11:14
peters252119-Oct-06 11:14 
GeneralExtGridView postback problem Pin
Chris Newstead10-Oct-06 12:34
Chris Newstead10-Oct-06 12:34 
GeneralRe: ExtGridView postback problem Pin
Cesare Esposito30-Dec-06 21:09
Cesare Esposito30-Dec-06 21:09 
AnswerRe: ExtGridView postback problem Pin
JL. ROBERT8-Feb-07 3:24
JL. ROBERT8-Feb-07 3:24 
GeneralDatabase Efficiency Pin
Paul Chu29-Sep-06 13:00
Paul Chu29-Sep-06 13:00 
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 

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.