Multi row header in Datagrid

W

William Gower

I have two columns in my datagrid, Hours Bid and Hours Expense. I would
like to have the word Hours span both columns and then just have the column
headings Bid and Expense underneath. Is that possible and how do I do that?
 
K

Ken Cox [Microsoft MVP]

Hi William,

During the PreRender event you can create a new datagrid header item. Add
one cell to it and tell the cell to span two columns. Add that to the
header. Insert this as the first item of the datagrid Here's some code:

Private Sub DataGrid1_PreRender _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles DataGrid1.PreRender
' Spans columns in an ASP.NET datagrid header
' Ken Cox Microsoft MVP [ASP.NET]
Dim dgitem As New DataGridItem _
(0, 0, ListItemType.Header)
Dim tblCell As New TableCell
tblCell.ColumnSpan = 2
tblCell.Text = "Hours"
dgitem.Cells.Add(tblCell)
DataGrid1.Controls(0).Controls.AddAt _
(0, dgitem)
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
W

William Gower

Thanks it almost works. I want it to appear over the 8 and 9th columns in
the header. Which value do I change for it to do that?

Ken Cox said:
Hi William,

During the PreRender event you can create a new datagrid header item. Add
one cell to it and tell the cell to span two columns. Add that to the
header. Insert this as the first item of the datagrid Here's some code:

Private Sub DataGrid1_PreRender _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles DataGrid1.PreRender
' Spans columns in an ASP.NET datagrid header
' Ken Cox Microsoft MVP [ASP.NET]
Dim dgitem As New DataGridItem _
(0, 0, ListItemType.Header)
Dim tblCell As New TableCell
tblCell.ColumnSpan = 2
tblCell.Text = "Hours"
dgitem.Cells.Add(tblCell)
DataGrid1.Controls(0).Controls.AddAt _
(0, dgitem)
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]


William Gower said:
I have two columns in my datagrid, Hours Bid and Hours Expense. I would
like to have the word Hours span both columns and then just have the
column
headings Bid and Expense underneath. Is that possible and how do I do
that?
 
K

Ken Cox [Microsoft MVP]

Hi William,

You just need to add some more dummy cells before the span. You could use
the same technique for adding dummy cells after the span.

Ken

Private Sub DataGrid1_PreRender _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles DataGrid1.PreRender
' Spans columns in an ASP.NET datagrid header
' Ken Cox Microsoft MVP [ASP.NET]
Dim intCounter As Integer
Dim dgitem As New DataGridItem _
(0, 0, ListItemType.Header)
Dim tblCell As TableCell
' Pad cells before the spanning cell
' Adjust the count as necessary
For intCounter = 0 To 6
tblCell = New TableCell
tblCell.ColumnSpan = 1
tblCell.Text = " "
dgitem.Cells.Add(tblCell)
Next
tblCell = New TableCell
tblCell.ColumnSpan = 2
tblCell.Text = "Hours"
dgitem.Cells.Add(tblCell)
DataGrid1.Controls(0).Controls.AddAt _
(0, dgitem)
End Sub
 
W

William Gower

thank you. It looks great. One small problem though. I have allowpaging.
But I only show the paging at the bottom of the page.

When it displays my page, I see the row that displays hours in the proper
column position, then I see the numeric page numbers then I see the other
row header.

Why is my paging showing up between the two rows?

Ken Cox said:
Hi William,

You just need to add some more dummy cells before the span. You could use
the same technique for adding dummy cells after the span.

Ken

Private Sub DataGrid1_PreRender _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles DataGrid1.PreRender
' Spans columns in an ASP.NET datagrid header
' Ken Cox Microsoft MVP [ASP.NET]
Dim intCounter As Integer
Dim dgitem As New DataGridItem _
(0, 0, ListItemType.Header)
Dim tblCell As TableCell
' Pad cells before the spanning cell
' Adjust the count as necessary
For intCounter = 0 To 6
tblCell = New TableCell
tblCell.ColumnSpan = 1
tblCell.Text = " "
dgitem.Cells.Add(tblCell)
Next
tblCell = New TableCell
tblCell.ColumnSpan = 2
tblCell.Text = "Hours"
dgitem.Cells.Add(tblCell)
DataGrid1.Controls(0).Controls.AddAt _
(0, dgitem)
End Sub

William Gower said:
Thanks it almost works. I want it to appear over the 8 and 9th columns in
the header. Which value do I change for it to do that?
 
W

William Gower

Also what if I want to add a third row header?

Ken Cox said:
Hi William,

You just need to add some more dummy cells before the span. You could use
the same technique for adding dummy cells after the span.

Ken

Private Sub DataGrid1_PreRender _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles DataGrid1.PreRender
' Spans columns in an ASP.NET datagrid header
' Ken Cox Microsoft MVP [ASP.NET]
Dim intCounter As Integer
Dim dgitem As New DataGridItem _
(0, 0, ListItemType.Header)
Dim tblCell As TableCell
' Pad cells before the spanning cell
' Adjust the count as necessary
For intCounter = 0 To 6
tblCell = New TableCell
tblCell.ColumnSpan = 1
tblCell.Text = " "
dgitem.Cells.Add(tblCell)
Next
tblCell = New TableCell
tblCell.ColumnSpan = 2
tblCell.Text = "Hours"
dgitem.Cells.Add(tblCell)
DataGrid1.Controls(0).Controls.AddAt _
(0, dgitem)
End Sub

William Gower said:
Thanks it almost works. I want it to appear over the 8 and 9th columns in
the header. Which value do I change for it to do that?
 
W

William Gower

Ok I solved this one.

William Gower said:
thank you. It looks great. One small problem though. I have allowpaging.
But I only show the paging at the bottom of the page.

When it displays my page, I see the row that displays hours in the proper
column position, then I see the numeric page numbers then I see the other
row header.

Why is my paging showing up between the two rows?

Ken Cox said:
Hi William,

You just need to add some more dummy cells before the span. You could use
the same technique for adding dummy cells after the span.

Ken

Private Sub DataGrid1_PreRender _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles DataGrid1.PreRender
' Spans columns in an ASP.NET datagrid header
' Ken Cox Microsoft MVP [ASP.NET]
Dim intCounter As Integer
Dim dgitem As New DataGridItem _
(0, 0, ListItemType.Header)
Dim tblCell As TableCell
' Pad cells before the spanning cell
' Adjust the count as necessary
For intCounter = 0 To 6
tblCell = New TableCell
tblCell.ColumnSpan = 1
tblCell.Text = " "
dgitem.Cells.Add(tblCell)
Next
tblCell = New TableCell
tblCell.ColumnSpan = 2
tblCell.Text = "Hours"
dgitem.Cells.Add(tblCell)
DataGrid1.Controls(0).Controls.AddAt _
(0, dgitem)
End Sub

William Gower said:
Thanks it almost works. I want it to appear over the 8 and 9th
columns
 
B

Bradley Plett

Well that's tantalizing!?! I'm running into exactly the same problem.
I'm glad you've solved it, but HOW?

Thanks!
Brad.

Ok I solved this one.

William Gower said:
thank you. It looks great. One small problem though. I have allowpaging.
But I only show the paging at the bottom of the page.

When it displays my page, I see the row that displays hours in the proper
column position, then I see the numeric page numbers then I see the other
row header.

Why is my paging showing up between the two rows?

Ken Cox said:
Hi William,

You just need to add some more dummy cells before the span. You could use
the same technique for adding dummy cells after the span.

Ken

Private Sub DataGrid1_PreRender _
(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles DataGrid1.PreRender
' Spans columns in an ASP.NET datagrid header
' Ken Cox Microsoft MVP [ASP.NET]
Dim intCounter As Integer
Dim dgitem As New DataGridItem _
(0, 0, ListItemType.Header)
Dim tblCell As TableCell
' Pad cells before the spanning cell
' Adjust the count as necessary
For intCounter = 0 To 6
tblCell = New TableCell
tblCell.ColumnSpan = 1
tblCell.Text = " "
dgitem.Cells.Add(tblCell)
Next
tblCell = New TableCell
tblCell.ColumnSpan = 2
tblCell.Text = "Hours"
dgitem.Cells.Add(tblCell)
DataGrid1.Controls(0).Controls.AddAt _
(0, dgitem)
End Sub

Thanks it almost works. I want it to appear over the 8 and 9th
columns
in
the header. Which value do I change for it to do that?
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top