Click here to Skip to main content
15,886,075 members
Articles / Web Development / IIS

Add an extra header to an ASP.NET DataGrid

Rate me:
Please Sign up or sign in to vote.
3.22/5 (3 votes)
9 Dec 2005CPOL 58.4K   204   19  
This article explains how to add an extra header item to a standard ASP.NET DataGrid.
Public Class _Default
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents g As WebApplication1.ExtraHeaderDataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            'bind the grid to a dummy datasource
            g.DataSource = GetSource()
            g.DataBind()
        End If
    End Sub

    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

    Private Sub btnRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Write("Refresh called from ExtraHeaderItem")
    End Sub

    'Get a dummy datasource
    Private Function GetSource() As DataTable
        Dim t As New DataTable

        'build columns
        t.Columns.Add("name")
        t.Columns.Add("age")
        t.Columns.Add("mothername")
        t.Columns.Add("fathername")

        Dim r As DataRow = t.NewRow
        r("name") = "David A"
        r("age") = 24
        r("mothername") = "Marcy A"
        r("fathername") = "Jimy A"
        t.Rows.Add(r)

        Dim r2 As DataRow = t.NewRow
        r2("name") = "Diana B"
        r2("age") = 30
        r2("mothername") = "Patricia A"
        r2("fathername") = "John B"
        t.Rows.Add(r2)

        Return t
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


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

Comments and Discussions