Click here to Skip to main content
6,305,776 members and growing! (16,204 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Add an extra header to the ASP.NET DataGrid

By Gabriel Sirbu

This article explains how to add an extra header item to a standard ASP.NET DataGrid
VB, .NET, Win2K, WinXP, Win2003, ASP.NET, Visual Studio, IIS 5.1, IIS 6, Dev
Posted:9 Dec 2005
Views:25,285
Bookmarked:17 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
3 votes for this article.
Popularity: 1.54 Rating: 3.22 out of 5

1

2
2 votes, 66.7%
3

4
1 vote, 33.3%
5

Sample Image - grid.jpg

Introduction

This article explains how to add an extra header item to a standard ASP.NET DataGrid.

Using the code

The code is pretty simple ,the grid is build using a DataGridTable object ,all we have to do is to add a DataGridItem to the rows collection in the CreateControlHierarchy method of our custom grid.
Now I'll let the code talk:

    Protected Overrides Sub CreateControlHierarchy(ByVal useDataSource As Boolean)
        MyBase.CreateControlHierarchy(useDataSource)
        If Not ShowExtraHeader Then Return
        If Controls.Count > 0 Then
            'get all grid's items (rows)
            Dim rows As TableRowCollection = DirectCast(Controls(0), Object).Rows
            'create extra header item
            Dim extraHeaderRow As DataGridItem = CreateItem(-1, -1, ListItemType.Header)
            'set an id that will identify it in ItemCreated event
            extraHeaderRow.ID = "extraheader"
            'create an event attached to our item
            Dim extraHeaderRowArgs As New DataGridItemEventArgs(extraHeaderRow)
            'call ItemCreated event
            OnItemCreated(extraHeaderRowArgs)
            'finally add the extra header item to the items's collection
            rows.AddAt(0, extraHeaderRow)
        End If
    End Sub
And let's see how could we use this code in a real scenario :
    Private Sub g_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles g.ItemCreated
        If e.Item.ItemType = ListItemType.Header Then
            If e.Item.ID = "extraheader" Then

                Dim refreshCell As New TableCell
                refreshCell.ColumnSpan = 2
                refreshCell.HorizontalAlign = HorizontalAlign.Center
                Dim btnRefresh As New Button
                btnRefresh.Text = "Refresh"
                refreshCell.Controls.Add(btnRefresh)
                AddHandler btnRefresh.Click, AddressOf btnRefresh_Click

                Dim parentsCell As New TableCell
                parentsCell.ColumnSpan = 2
                parentsCell.HorizontalAlign = HorizontalAlign.Center
                parentsCell.Text = "Parents"

                e.Item.Cells.Add(refreshCell)
                e.Item.Cells.Add(parentsCell)
            End If
        End If
    End Sub

That's all!Check the demo project to see all the bits.

History

Dec 10, 2005 - Version 1.0.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gabriel Sirbu


Member

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Generalhow do i remove paging [modified] Pinmembermaya_zakry23:47 3 Jun '07  
GeneralItem_Databoud Event Pinmemberjhkss10:49 2 Nov '06  
GeneralA Better Alternative. PinmemberMiguel_TX19:40 29 Apr '06  
GeneralRe: A Better Alternative. PinmemberGabriel Sirbu21:33 30 Apr '06  
GeneralRe: A Better Alternative. PinmemberJoel Pearson23:04 22 May '06  
GeneralRe: A Better Alternative. PinmemberMiguel_TX21:32 12 Jun '06  
GeneralRe: A Better Alternative. Pinmemberdga2055:41 22 Jun '06  
Generalc# PinmemberMark Lawson10:39 29 Dec '05  
GeneralRe: c# Pinmembersyao16811:07 25 Feb '06  
Generalc# PinmemberMark Lawson10:38 29 Dec '05  

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

PermaLink | Privacy | Terms of Use
Last Updated: 9 Dec 2005
Editor:
Copyright 2005 by Gabriel Sirbu
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project