Professional Web Applications Themes

datagrid header - ASP.NET Data Grid Control

Is it possible to have an overall header that spans across all columns? An example would be a class schedule for student Ben. |Ben |English |Thursday | |Ben |Geology |Monday | |Ben |Physics |Tuesday | I want it to look like this: | Ben | |English |Thursday | |Geology |Monday | |Physics |Tuesday | In other words, hide the student column and make it a non-repeatable value in the datagrid header. Note, each datagrid would have only one student name. Thanks for any help....

  1. #1

    Default datagrid header

    Is it possible to have an overall header that spans across all
    columns?
    An example would be a class schedule for student Ben.

    |Ben |English |Thursday |
    |Ben |Geology |Monday |
    |Ben |Physics |Tuesday |

    I want it to look like this:

    | Ben |

    |English |Thursday |
    |Geology |Monday |
    |Physics |Tuesday |

    In other words, hide the student column and make it a non-repeatable
    value in the datagrid header. Note, each datagrid would have only one
    student name. Thanks for any help.
    Cambridgeways Guest

  2. #2

    Default Re: datagrid header

    Here's one possibility... create your own header and put the student's name
    in it. Then, just display the two columns.

    Dim dt As DataTable
    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End Sub

    Private Sub DataGrid1_ItemDataBound _
    (ByVal sender As Object, _
    ByVal e As _
    System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles DataGrid1.ItemDataBound
    ' Add the header and text
    If e.Item.ItemType = ListItemType.Header Then
    ' Get the collection of cells from the grid
    Dim tcells As TableCellCollection
    tcells = e.Item.Cells
    ' Create a new cell
    Dim fcell As New TableCell
    Dim lblHeader As New Label
    ' Assign text to the label
    lblHeader.Text = dt.Rows(0).Item(0)
    fcell.Controls.Add(lblHeader)
    ' Span the cell to however many columns there are
    fcell.ColumnSpan = tcells.Count
    ' Create a new footer object
    Dim dgItemHeader As New DataGridItem _
    (0, 0, ListItemType.Header)
    Dim dgItemOldHeader As DataGridItem
    ' Create an instance of the header
    'from the current header object
    dgItemOldHeader = e.Item
    ' Delete the existing header
    DataGrid1.Controls(0).Controls.Remove(dgItemOldHea der)
    ' Add the cell to the header
    dgItemHeader.Cells.Add(fcell)
    dgItemHeader.Visible = True
    ' Add the header to the datagrid
    DataGrid1.Controls(0).Controls.Add(dgItemHeader)
    End If
    End Sub
    Function CreateDataSource() As ICollection
    ' Create sample data for the DataList control.
    dt = New DataTable
    Dim dr As DataRow
    ' Define the columns of the table.
    dt.Columns.Add(New DataColumn("Student", GetType(String)))
    dt.Columns.Add(New DataColumn("Subject", GetType(String)))
    dt.Columns.Add(New DataColumn("Day", GetType(String)))
    ' Populate the table with sample values.
    dr = dt.NewRow
    dr(0) = "Ben"
    dr(1) = "English"
    dr(2) = "Thursday"
    dt.Rows.Add(dr)
    dr = dt.NewRow
    dr(0) = "Ben"
    dr(1) = "Geology"
    dr(2) = "Monday"
    dt.Rows.Add(dr)
    dr = dt.NewRow
    dr(0) = "Ben"
    dr(1) = "Physics"
    dr(2) = "Tuesday"
    dt.Rows.Add(dr)
    Dim dv As DataView = New DataView(dt)
    Return dv
    End Function

    <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="Subject"></asp:BoundColumn>
    <asp:BoundColumn DataField="Day"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>

    "Cambridgeways" <cambridgewayshotmail.com> wrote in message
    news:e0774553.0401161258.10fe981posting.google.co m...
    > Is it possible to have an overall header that spans across all
    > columns?
    > An example would be a class schedule for student Ben.
    >
    > |Ben |English |Thursday |
    > |Ben |Geology |Monday |
    > |Ben |Physics |Tuesday |
    >
    > I want it to look like this:
    >
    > | Ben |
    >
    > |English |Thursday |
    > |Geology |Monday |
    > |Physics |Tuesday |
    >
    > In other words, hide the student column and make it a non-repeatable
    > value in the datagrid header. Note, each datagrid would have only one
    > student name. Thanks for any help.
    Ken Cox [Microsoft MVP] Guest

  3. #3

    Default Re: datagrid header

    Thanks for your time and attention. I ended up doing something very similar.

    "Ken Cox [Microsoft MVP]" <BANSPAMken_coxsympatico.ca> wrote in message news:<uQ85Nyv3DHA.3360tk2msftngp13.phx.gbl>...
    > Here's one possibility... create your own header and put the student's name
    > in it. Then, just display the two columns.
    >
    > Dim dt As DataTable
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > DataGrid1.DataSource = CreateDataSource()
    > DataGrid1.DataBind()
    > End Sub
    >
    > Private Sub DataGrid1_ItemDataBound _
    > (ByVal sender As Object, _
    > ByVal e As _
    > System.Web.UI.WebControls.DataGridItemEventArgs) _
    > Handles DataGrid1.ItemDataBound
    > ' Add the header and text
    > If e.Item.ItemType = ListItemType.Header Then
    > ' Get the collection of cells from the grid
    > Dim tcells As TableCellCollection
    > tcells = e.Item.Cells
    > ' Create a new cell
    > Dim fcell As New TableCell
    > Dim lblHeader As New Label
    > ' Assign text to the label
    > lblHeader.Text = dt.Rows(0).Item(0)
    > fcell.Controls.Add(lblHeader)
    > ' Span the cell to however many columns there are
    > fcell.ColumnSpan = tcells.Count
    > ' Create a new footer object
    > Dim dgItemHeader As New DataGridItem _
    > (0, 0, ListItemType.Header)
    > Dim dgItemOldHeader As DataGridItem
    > ' Create an instance of the header
    > 'from the current header object
    > dgItemOldHeader = e.Item
    > ' Delete the existing header
    > DataGrid1.Controls(0).Controls.Remove(dgItemOldHea der)
    > ' Add the cell to the header
    > dgItemHeader.Cells.Add(fcell)
    > dgItemHeader.Visible = True
    > ' Add the header to the datagrid
    > DataGrid1.Controls(0).Controls.Add(dgItemHeader)
    > End If
    > End Sub
    > Function CreateDataSource() As ICollection
    > ' Create sample data for the DataList control.
    > dt = New DataTable
    > Dim dr As DataRow
    > ' Define the columns of the table.
    > dt.Columns.Add(New DataColumn("Student", GetType(String)))
    > dt.Columns.Add(New DataColumn("Subject", GetType(String)))
    > dt.Columns.Add(New DataColumn("Day", GetType(String)))
    > ' Populate the table with sample values.
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "English"
    > dr(2) = "Thursday"
    > dt.Rows.Add(dr)
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "Geology"
    > dr(2) = "Monday"
    > dt.Rows.Add(dr)
    > dr = dt.NewRow
    > dr(0) = "Ben"
    > dr(1) = "Physics"
    > dr(2) = "Tuesday"
    > dt.Rows.Add(dr)
    > Dim dv As DataView = New DataView(dt)
    > Return dv
    > End Function
    >
    > <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
    > <Columns>
    > <asp:BoundColumn DataField="Subject"></asp:BoundColumn>
    > <asp:BoundColumn DataField="Day"></asp:BoundColumn>
    > </Columns>
    > </asp:DataGrid>
    >
    > "Cambridgeways" <cambridgewayshotmail.com> wrote in message
    > news:e0774553.0401161258.10fe981posting.google.co m...
    > > Is it possible to have an overall header that spans across all
    > > columns?
    > > An example would be a class schedule for student Ben.
    > >
    > > |Ben |English |Thursday |
    > > |Ben |Geology |Monday |
    > > |Ben |Physics |Tuesday |
    > >
    > > I want it to look like this:
    > >
    > > | Ben |
    >
    > > |English |Thursday |
    > > |Geology |Monday |
    > > |Physics |Tuesday |
    > >
    > > In other words, hide the student column and make it a non-repeatable
    > > value in the datagrid header. Note, each datagrid would have only one
    > > student name. Thanks for any help.
    Cambridgeways Guest

Similar Threads

  1. Droplist in Header of DataGrid. Possible?
    By hoosierprogrammer@yahoo.com in forum ASP.NET Data Grid Control
    Replies: 5
    Last Post: September 7th, 09:23 PM
  2. Having 2-row customized header within the DataGrid
    By Martin Raychev in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: March 2nd, 02:59 PM
  3. Edit DataGrid Header
    By Mike Smith in forum ASP.NET Data Grid Control
    Replies: 4
    Last Post: September 21st, 07:35 PM
  4. PreRender Datagrid Header
    By Nagul in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: July 26th, 02:13 PM
  5. DataGrid header and <TH>
    By Bassel Tabbara [MSFT] in forum ASP.NET General
    Replies: 1
    Last Post: June 27th, 07:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139