473,503 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

datagrid - 2 header columns

i need a datagrid with 2 header columns. The top one might have 1
column spanning 5 columns of the header row below it.

what is the best way to do this? Could i have 2 datatables...1 filling
the top row, and the 2nd header row would come from the 2nd datatable?
In this case, i guess i would have to add a row manually above the
header row (2nd dataset) which is the set of 1st data?

if someone has ideas, code explaining it would be very helpful.

Nov 23 '05 #1
4 4704
Here's a script that should get you going. Let us know?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<script runat="server">
Dim dt As Data.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
' Have the first column header span
' two columns
' by Ken Cox Microsoft MVP [ASP.NET]
If e.Item.ItemType = ListItemType.Header Then
' Declare variables
Dim dgItem As DataGridItem
Dim tcells As TableCellCollection
Dim fcell As TableCell
Dim scell As TableCell
' Get a reference to the header row item
dgItem = e.Item
' Get a reference to the cells in the
' header row item
tcells = e.Item.Cells
' Get a reference to the cell we want to
' span. In this case, the first cell
fcell = e.Item.Cells(0)
' Set the text of the span cell
fcell.Text = "This is the spanned cell"
' Set the columns to span to 2
fcell.ColumnSpan = 2
' Get a reference to the second cell
scell = e.Item.Cells(1)
' Remove the second cell because it will
' be replaced by the spanning column
dgItem.Cells.Remove(scell)
End If
End Sub
Function CreateDataSource() As ICollection
' Create sample data for the DataList control.
dt = New Data.DataTable
Dim dr As Data.DataRow
' Define the columns of the table.
dt.Columns.Add(New Data.DataColumn("Student", GetType(String)))
dt.Columns.Add(New Data.DataColumn("Subject", GetType(String)))
dt.Columns.Add(New Data.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 Data.DataView = New Data.DataView(dt)
Return dv
End Function

</script>

<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datagrid ID="DataGrid1" runat="server">
</asp:datagrid>
</div>
</form>
</body>
</html>

"Mortar" <a@b.com> wrote in message
news:43***************@nntp.broadband.rogers.com.. .
i need a datagrid with 2 header columns. The top one might have 1
column spanning 5 columns of the header row below it.

what is the best way to do this? Could i have 2 datatables...1 filling
the top row, and the 2nd header row would come from the 2nd datatable?
In this case, i guess i would have to add a row manually above the
header row (2nd dataset) which is the set of 1st data?

if someone has ideas, code explaining it would be very helpful.


Nov 23 '05 #2

Thanks Ken for the code. Much appreciated and useful. I understand
what you are doing, but am contemplating using no header on the grid
at all.

I am thinking of using 3 unions in my query (the 1st two being the
header rows, the 3rd being the dataset), then just manually changing
the background color of the 1st two rows to make them look like header
rows. What do you think?
On Tue, 22 Nov 2005 23:13:07 -0800, "Ken Cox [Microsoft MVP]"
<BA************@sympatico.ca> wrote:
Here's a script that should get you going. Let us know?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<script runat="server">
Dim dt As Data.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
' Have the first column header span
' two columns
' by Ken Cox Microsoft MVP [ASP.NET]
If e.Item.ItemType = ListItemType.Header Then
' Declare variables
Dim dgItem As DataGridItem
Dim tcells As TableCellCollection
Dim fcell As TableCell
Dim scell As TableCell
' Get a reference to the header row item
dgItem = e.Item
' Get a reference to the cells in the
' header row item
tcells = e.Item.Cells
' Get a reference to the cell we want to
' span. In this case, the first cell
fcell = e.Item.Cells(0)
' Set the text of the span cell
fcell.Text = "This is the spanned cell"
' Set the columns to span to 2
fcell.ColumnSpan = 2
' Get a reference to the second cell
scell = e.Item.Cells(1)
' Remove the second cell because it will
' be replaced by the spanning column
dgItem.Cells.Remove(scell)
End If
End Sub
Function CreateDataSource() As ICollection
' Create sample data for the DataList control.
dt = New Data.DataTable
Dim dr As Data.DataRow
' Define the columns of the table.
dt.Columns.Add(New Data.DataColumn("Student", GetType(String)))
dt.Columns.Add(New Data.DataColumn("Subject", GetType(String)))
dt.Columns.Add(New Data.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 Data.DataView = New Data.DataView(dt)
Return dv
End Function

</script>

<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datagrid ID="DataGrid1" runat="server">
</asp:datagrid>
</div>
</form>
</body>
</html>

"Mortar" <a@b.com> wrote in message
news:43***************@nntp.broadband.rogers.com. ..
i need a datagrid with 2 header columns. The top one might have 1
column spanning 5 columns of the header row below it.

what is the best way to do this? Could i have 2 datatables...1 filling
the top row, and the 2nd header row would come from the 2nd datatable?
In this case, i guess i would have to add a row manually above the
header row (2nd dataset) which is the set of 1st data?

if someone has ideas, code explaining it would be very helpful.


Nov 23 '05 #3

hmm, om second thought looks like i'll have to do it your way....
On Wed, 23 Nov 2005 16:59:32 GMT, a@b.com (Mortar) wrote:

Thanks Ken for the code. Much appreciated and useful. I understand
what you are doing, but am contemplating using no header on the grid
at all.

I am thinking of using 3 unions in my query (the 1st two being the
header rows, the 3rd being the dataset), then just manually changing
the background color of the 1st two rows to make them look like header
rows. What do you think?
On Tue, 22 Nov 2005 23:13:07 -0800, "Ken Cox [Microsoft MVP]"
<BA************@sympatico.ca> wrote:
Here's a script that should get you going. Let us know?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<script runat="server">
Dim dt As Data.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
' Have the first column header span
' two columns
' by Ken Cox Microsoft MVP [ASP.NET]
If e.Item.ItemType = ListItemType.Header Then
' Declare variables
Dim dgItem As DataGridItem
Dim tcells As TableCellCollection
Dim fcell As TableCell
Dim scell As TableCell
' Get a reference to the header row item
dgItem = e.Item
' Get a reference to the cells in the
' header row item
tcells = e.Item.Cells
' Get a reference to the cell we want to
' span. In this case, the first cell
fcell = e.Item.Cells(0)
' Set the text of the span cell
fcell.Text = "This is the spanned cell"
' Set the columns to span to 2
fcell.ColumnSpan = 2
' Get a reference to the second cell
scell = e.Item.Cells(1)
' Remove the second cell because it will
' be replaced by the spanning column
dgItem.Cells.Remove(scell)
End If
End Sub
Function CreateDataSource() As ICollection
' Create sample data for the DataList control.
dt = New Data.DataTable
Dim dr As Data.DataRow
' Define the columns of the table.
dt.Columns.Add(New Data.DataColumn("Student", GetType(String)))
dt.Columns.Add(New Data.DataColumn("Subject", GetType(String)))
dt.Columns.Add(New Data.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 Data.DataView = New Data.DataView(dt)
Return dv
End Function

</script>

<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datagrid ID="DataGrid1" runat="server">
</asp:datagrid>
</div>
</form>
</body>
</html>

"Mortar" <a@b.com> wrote in message
news:43***************@nntp.broadband.rogers.com ...
i need a datagrid with 2 header columns. The top one might have 1
column spanning 5 columns of the header row below it.

what is the best way to do this? Could i have 2 datatables...1 filling
the top row, and the 2nd header row would come from the 2nd datatable?
In this case, i guess i would have to add a row manually above the
header row (2nd dataset) which is the set of 1st data?

if someone has ideas, code explaining it would be very helpful.


Nov 23 '05 #4
i goofed. My original post...i meant 2 header ROWS not columns. Sorry.

So the grid would look something like the following:

'', '', how many kids do you have?
firstname, lastname, no kids, 1 kid, 2 kids, more than 3 kids
Adam, Ant, 0, 0, 1, 0

so you can see, the top row...the question needs to span the possible
answers in the row below it. And the response would be a 1 or 0 (yes
or no)

so the top 2 rows are both headers, and then the 3rd and onward are
the data.

On Tue, 22 Nov 2005 23:13:07 -0800, "Ken Cox [Microsoft MVP]"
<BA************@sympatico.ca> wrote:
Here's a script that should get you going. Let us know?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>
<script runat="server">
Dim dt As Data.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
' Have the first column header span
' two columns
' by Ken Cox Microsoft MVP [ASP.NET]
If e.Item.ItemType = ListItemType.Header Then
' Declare variables
Dim dgItem As DataGridItem
Dim tcells As TableCellCollection
Dim fcell As TableCell
Dim scell As TableCell
' Get a reference to the header row item
dgItem = e.Item
' Get a reference to the cells in the
' header row item
tcells = e.Item.Cells
' Get a reference to the cell we want to
' span. In this case, the first cell
fcell = e.Item.Cells(0)
' Set the text of the span cell
fcell.Text = "This is the spanned cell"
' Set the columns to span to 2
fcell.ColumnSpan = 2
' Get a reference to the second cell
scell = e.Item.Cells(1)
' Remove the second cell because it will
' be replaced by the spanning column
dgItem.Cells.Remove(scell)
End If
End Sub
Function CreateDataSource() As ICollection
' Create sample data for the DataList control.
dt = New Data.DataTable
Dim dr As Data.DataRow
' Define the columns of the table.
dt.Columns.Add(New Data.DataColumn("Student", GetType(String)))
dt.Columns.Add(New Data.DataColumn("Subject", GetType(String)))
dt.Columns.Add(New Data.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 Data.DataView = New Data.DataView(dt)
Return dv
End Function

</script>

<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datagrid ID="DataGrid1" runat="server">
</asp:datagrid>
</div>
</form>
</body>
</html>

"Mortar" <a@b.com> wrote in message
news:43***************@nntp.broadband.rogers.com. ..
i need a datagrid with 2 header columns. The top one might have 1
column spanning 5 columns of the header row below it.

what is the best way to do this? Could i have 2 datatables...1 filling
the top row, and the 2nd header row would come from the 2nd datatable?
In this case, i guess i would have to add a row manually above the
header row (2nd dataset) which is the set of 1st data?

if someone has ideas, code explaining it would be very helpful.


Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2494
by: Ben | last post by:
Hi, I'm wondering how can i change the datagrid header text at runtime so that it keeps the sorting enabled? I can do; e.Item.Cells.Text = "foo"; But then foo is the text of the table cell...
2
1720
by: rooster575 | last post by:
I would like my datagrid to have the following format: ID - Name - Phone Title - Birthday - email Is this possible for the header and result items? Thanks.
2
4224
by: saleek | last post by:
I was wondering if there is a way I can add an extra header to a datagrid? I found this solution on the internet - but it seems quite old and didn't work for me. ...
1
3425
by: sri_san | last post by:
Hello, I have a datagrid in which the header needs to span over 2 columns. I have tried creating a tableCells and tableRow at runtime and set the columnspan property of a cell to 2. But, the...
2
6374
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
0
1812
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
7
7441
by: GaryDean | last post by:
I am writing a method in a component that gets passed a 1.1 Datagrid. The datagrid's columns were created at run time meaning it has no columns collection. How can I access Header and Footer...
0
2707
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
2
6559
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.