Professional Web Applications Themes

Two column header rows possible? - ASP.NET Data Grid Control

We are currently creating output displaying 'Total' and 'Avg' information. Is it possible to add a row above, spanning multiple columns similar to this?: | Widgets | +----------------+ | Total | Avg | Thanks Bill...

  1. #1

    Default Two column header rows possible?

    We are currently creating output displaying 'Total' and 'Avg' information.
    Is it possible to add a row above, spanning multiple columns similar to
    this?:

    | Widgets |
    +----------------+
    | Total | Avg |

    Thanks
    Bill


    Bill Musgrave Guest

  2. #2

    Default Re: Two column header rows possible?

    It is not supported directly, but I can think of faking it by inserting
    another data row with subheaders into the data table. You can format in a
    special way making it look differently from data items. You can use
    PreRender event for formatting.

    Eliyahu

    "Bill Musgrave" <Musgrave_BillNOSPA_Memc.com> wrote in message
    news:%23D8fNnV4EHA.208TK2MSFTNGP12.phx.gbl...
    > We are currently creating output displaying 'Total' and 'Avg' information.
    > Is it possible to add a row above, spanning multiple columns similar to
    > this?:
    >
    > | Widgets |
    > +----------------+
    > | Total | Avg |
    >
    > Thanks
    > Bill
    >
    >

    Eliyahu Goldin Guest

  3. #3

    Default Re: Two column header rows possible?

    Thanks Eiliyahu, though I am not sure I understand.

    When you said data row, did you mean add another datagridcontrol? If so how
    do we get all the columns to align?

    If not, maybe you can elaborate (any code would be helpful).

    Also, would there be a way to do this utilizing the templatecolumn?

    Thanks again.
    Bill


    "Eliyahu Goldin" <removemeegoldinmonarchmed.com> wrote in message
    news:eeszDjb4EHA.3316tk2msftngp13.phx.gbl...
    > It is not supported directly, but I can think of faking it by inserting
    > another data row with subheaders into the data table. You can format in a
    > special way making it look differently from data items. You can use
    > PreRender event for formatting.
    >
    > Eliyahu
    >
    > "Bill Musgrave" <Musgrave_BillNOSPA_Memc.com> wrote in message
    > news:%23D8fNnV4EHA.208TK2MSFTNGP12.phx.gbl...
    > > We are currently creating output displaying 'Total' and 'Avg'
    information.
    > > Is it possible to add a row above, spanning multiple columns similar to
    > > this?:
    > >
    > > | Widgets |
    > > +----------------+
    > > | Total | Avg |
    > >
    > > Thanks
    > > Bill
    > >
    > >
    >
    >

    Bill Musgrave Guest

  4. #4

    Default Re: Two column header rows possible?

    Hi Bill,

    Below, I've pasted in some code that should help you create headers that
    span. It is a little finicky but can be done. See the code comments for
    details.

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto


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


    Dim dt As DataTable
    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    DataGrid1.ShowHeader = True
    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 a second header with a flag button in it
    ' by Ken Cox Microsoft MVP [ASP.NET]
    If e.Item.ItemType = ListItemType.Header Then
    ' Get the collection of cells from the grid
    Dim tcells As TableCellCollection
    ' Create an imagebutton
    Dim imgBtn As New ImageButton
    ' Assign the URL
    imgBtn.ImageUrl = "http://www.gc.ca/images/flag.gif"
    ' Get the collection of existing cells so we can get a count
    tcells = e.Item.Cells
    ' Create a new cell
    Dim fcell As New TableCell
    ' Add the image button to the new table cell
    fcell.Controls.Add(imgBtn)
    ' Span the cell to however many columns there are
    fcell.ColumnSpan = tcells.Count
    ' Create a new header object
    Dim dgItemHeader As New DataGridItem _
    (0, 0, ListItemType.Header)
    ' 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

    "Bill Musgrave" <Musgrave_BillNOSPA_Memc.com> wrote in message
    news:%23D8fNnV4EHA.208TK2MSFTNGP12.phx.gbl...
    > We are currently creating output displaying 'Total' and 'Avg' information.
    > Is it possible to add a row above, spanning multiple columns similar to
    > this?:
    >
    > | Widgets |
    > +----------------+
    > | Total | Avg |
    >
    > Thanks
    > Bill
    >
    >
    Ken Cox [Microsoft MVP] Guest

  5. #5

    Default Re: Two column header rows possible?

    Bill,

    I mean adding another data row to the data table. When you bind the grid to
    the table, the row should be rendered straight after the header.

    Yes, template columns can be used. You can prepare HTML in ItemTemplate for
    both subheader and normal data and, in ItemDataBound event, check on the
    very first data row. If an item is the very first data row, populate HTML
    for subheaders.

    Eliyahu

    "Bill Musgrave" <Musgrave_BillNOSPA_Memc.com> wrote in message
    news:Oniy7te4EHA.2124TK2MSFTNGP15.phx.gbl...
    > Thanks Eiliyahu, though I am not sure I understand.
    >
    > When you said data row, did you mean add another datagridcontrol? If so
    how
    > do we get all the columns to align?
    >
    > If not, maybe you can elaborate (any code would be helpful).
    >
    > Also, would there be a way to do this utilizing the templatecolumn?
    >
    > Thanks again.
    > Bill
    >
    >
    > "Eliyahu Goldin" <removemeegoldinmonarchmed.com> wrote in message
    > news:eeszDjb4EHA.3316tk2msftngp13.phx.gbl...
    > > It is not supported directly, but I can think of faking it by inserting
    > > another data row with subheaders into the data table. You can format in
    a
    > > special way making it look differently from data items. You can use
    > > PreRender event for formatting.
    > >
    > > Eliyahu
    > >
    > > "Bill Musgrave" <Musgrave_BillNOSPA_Memc.com> wrote in message
    > > news:%23D8fNnV4EHA.208TK2MSFTNGP12.phx.gbl...
    > > > We are currently creating output displaying 'Total' and 'Avg'
    > information.
    > > > Is it possible to add a row above, spanning multiple columns similar
    to
    > > > this?:
    > > >
    > > > | Widgets |
    > > > +----------------+
    > > > | Total | Avg |
    > > >
    > > > Thanks
    > > > Bill
    > > >
    > > >
    > >
    > >
    >
    >

    Eliyahu Goldin Guest

  6. #6

    Default Re: Two column header rows possible?

    Ken thanks for your response, it helped tons.

    Merry Christmas
    Bill

    "Ken Cox [Microsoft MVP]" <BANSPAMken_coxsympatico.ca> wrote in message
    news:O4vASCl4EHA.4004tk2msftngp13.phx.gbl...
    > Hi Bill,
    >
    > Below, I've pasted in some code that should help you create headers that
    > span. It is a little finicky but can be done. See the code comments for
    > details.
    >
    > Let us know if this helps?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    > Toronto
    >
    >
    > <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False"
    > ShowHeader="False">
    > <Columns>
    > <asp:BoundColumn DataField="Subject"></asp:BoundColumn>
    > <asp:BoundColumn DataField="Day"></asp:BoundColumn>
    > </Columns>
    > </asp:DataGrid>
    >
    >
    > Dim dt As DataTable
    > Private Sub Page_Load _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles MyBase.Load
    > DataGrid1.ShowHeader = True
    > 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 a second header with a flag button in it
    > ' by Ken Cox Microsoft MVP [ASP.NET]
    > If e.Item.ItemType = ListItemType.Header Then
    > ' Get the collection of cells from the grid
    > Dim tcells As TableCellCollection
    > ' Create an imagebutton
    > Dim imgBtn As New ImageButton
    > ' Assign the URL
    > imgBtn.ImageUrl = "http://www.gc.ca/images/flag.gif"
    > ' Get the collection of existing cells so we can get a count
    > tcells = e.Item.Cells
    > ' Create a new cell
    > Dim fcell As New TableCell
    > ' Add the image button to the new table cell
    > fcell.Controls.Add(imgBtn)
    > ' Span the cell to however many columns there are
    > fcell.ColumnSpan = tcells.Count
    > ' Create a new header object
    > Dim dgItemHeader As New DataGridItem _
    > (0, 0, ListItemType.Header)
    > ' 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
    >
    > "Bill Musgrave" <Musgrave_BillNOSPA_Memc.com> wrote in message
    > news:%23D8fNnV4EHA.208TK2MSFTNGP12.phx.gbl...
    > > We are currently creating output displaying 'Total' and 'Avg'
    information.
    > > Is it possible to add a row above, spanning multiple columns similar to
    > > this?:
    > >
    > > | Widgets |
    > > +----------------+
    > > | Total | Avg |
    > >
    > > Thanks
    > > Bill
    > >
    > >
    >

    Bill Musgrave Guest

  7. #7

    Default Re: Two column header rows possible?

    Eilyahu

    Thanks for your time, we were finally able to get things working.

    Merry Christmas
    Bill

    "Eliyahu Goldin" <removemeegoldinmonarchmed.com> wrote in message
    news:Olsu3Ip4EHA.2964TK2MSFTNGP15.phx.gbl...
    > Bill,
    >
    > I mean adding another data row to the data table. When you bind the grid
    to
    > the table, the row should be rendered straight after the header.
    >
    > Yes, template columns can be used. You can prepare HTML in ItemTemplate
    for
    > both subheader and normal data and, in ItemDataBound event, check on the
    > very first data row. If an item is the very first data row, populate HTML
    > for subheaders.
    >
    > Eliyahu
    >
    > "Bill Musgrave" <Musgrave_BillNOSPA_Memc.com> wrote in message
    > news:Oniy7te4EHA.2124TK2MSFTNGP15.phx.gbl...
    > > Thanks Eiliyahu, though I am not sure I understand.
    > >
    > > When you said data row, did you mean add another datagridcontrol? If so
    > how
    > > do we get all the columns to align?
    > >
    > > If not, maybe you can elaborate (any code would be helpful).
    > >
    > > Also, would there be a way to do this utilizing the templatecolumn?
    > >
    > > Thanks again.
    > > Bill
    > >
    > >
    > > "Eliyahu Goldin" <removemeegoldinmonarchmed.com> wrote in message
    > > news:eeszDjb4EHA.3316tk2msftngp13.phx.gbl...
    > > > It is not supported directly, but I can think of faking it by
    inserting
    > > > another data row with subheaders into the data table. You can format
    in
    > a
    > > > special way making it look differently from data items. You can use
    > > > PreRender event for formatting.
    > > >
    > > > Eliyahu
    > > >
    > > > "Bill Musgrave" <Musgrave_BillNOSPA_Memc.com> wrote in message
    > > > news:%23D8fNnV4EHA.208TK2MSFTNGP12.phx.gbl...
    > > > > We are currently creating output displaying 'Total' and 'Avg'
    > > information.
    > > > > Is it possible to add a row above, spanning multiple columns similar
    > to
    > > > > this?:
    > > > >
    > > > > | Widgets |
    > > > > +----------------+
    > > > > | Total | Avg |
    > > > >
    > > > > Thanks
    > > > > Bill
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    Bill Musgrave Guest

Similar Threads

  1. Datagrid with multiple *header/category* rows
    By ivan.svaljek@gmail.com in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: September 12th, 05:00 PM
  2. Can datagrid header text displayed more than one rows
    By wzy2345 in forum Macromedia Flex General Discussion
    Replies: 4
    Last Post: November 9th, 11:09 PM
  3. Repeat Header every 10 rows
    By eval in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: November 8th, 11:14 PM
  4. Multiple Header rows in datagrid
    By Alfred Salton in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: July 15th, 09:16 PM
  5. Image in header column (not replacing column header text)
    By hansiman in forum ASP.NET Data Grid Control
    Replies: 3
    Last Post: February 7th, 12:17 AM

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