datagrid header

C

Cambridgeways

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.
 
K

Ken Cox [Microsoft MVP]

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(dgItemOldHeader)
' 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,815
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top