473,842 Members | 1,622 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 4728
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.EventArg s) _
Handles MyBase.Load
DataGrid1.ShowH eader = True
DataGrid1.DataS ource = CreateDataSourc e()
DataGrid1.DataB ind()
End Sub
Private Sub DataGrid1_ItemD ataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.W ebControls.Data GridItemEventAr gs) _
Handles DataGrid1.ItemD ataBound
' Have the first column header span
' two columns
' by Ken Cox Microsoft MVP [ASP.NET]
If e.Item.ItemType = ListItemType.He ader Then
' Declare variables
Dim dgItem As DataGridItem
Dim tcells As TableCellCollec tion
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.ColumnSpa n = 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.Re move(scell)
End If
End Sub
Function CreateDataSourc e() 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(d t)
Return dv
End Function

</script>

<html>
<head runat="server">
<title>Untitl ed 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.br oadband.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.EventArg s) _
Handles MyBase.Load
DataGrid1.ShowH eader = True
DataGrid1.DataS ource = CreateDataSourc e()
DataGrid1.DataB ind()
End Sub
Private Sub DataGrid1_ItemD ataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.W ebControls.Data GridItemEventAr gs) _
Handles DataGrid1.ItemD ataBound
' Have the first column header span
' two columns
' by Ken Cox Microsoft MVP [ASP.NET]
If e.Item.ItemType = ListItemType.He ader Then
' Declare variables
Dim dgItem As DataGridItem
Dim tcells As TableCellCollec tion
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.ColumnSpa n = 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.Re move(scell)
End If
End Sub
Function CreateDataSourc e() 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(d t)
Return dv
End Function

</script>

<html>
<head runat="server">
<title>Untitl ed 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.b roadband.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.EventArg s) _
Handles MyBase.Load
DataGrid1.ShowH eader = True
DataGrid1.DataS ource = CreateDataSourc e()
DataGrid1.DataB ind()
End Sub
Private Sub DataGrid1_ItemD ataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.W ebControls.Data GridItemEventAr gs) _
Handles DataGrid1.ItemD ataBound
' Have the first column header span
' two columns
' by Ken Cox Microsoft MVP [ASP.NET]
If e.Item.ItemType = ListItemType.He ader Then
' Declare variables
Dim dgItem As DataGridItem
Dim tcells As TableCellCollec tion
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.ColumnSpa n = 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.Re move(scell)
End If
End Sub
Function CreateDataSourc e() 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(d t)
Return dv
End Function

</script>

<html>
<head runat="server">
<title>Untitl ed 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.roger s.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.EventArg s) _
Handles MyBase.Load
DataGrid1.ShowH eader = True
DataGrid1.DataS ource = CreateDataSourc e()
DataGrid1.DataB ind()
End Sub
Private Sub DataGrid1_ItemD ataBound _
(ByVal sender As Object, _
ByVal e As _
System.Web.UI.W ebControls.Data GridItemEventAr gs) _
Handles DataGrid1.ItemD ataBound
' Have the first column header span
' two columns
' by Ken Cox Microsoft MVP [ASP.NET]
If e.Item.ItemType = ListItemType.He ader Then
' Declare variables
Dim dgItem As DataGridItem
Dim tcells As TableCellCollec tion
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.ColumnSpa n = 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.Re move(scell)
End If
End Sub
Function CreateDataSourc e() 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(d t)
Return dv
End Function

</script>

<html>
<head runat="server">
<title>Untitl ed 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.b roadband.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
2523
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 and not the underlying hyperlink and as such, foo is no longer sortable.
2
1732
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
4237
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. http://www.dotnet247.com/247reference/msgs/13/69744.aspx It recommends the following: Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
1
3450
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 heading does not look good and is not aligned to the datagrid columns. Is there another way to do it? Any help would be great!! Thanks, Sam.
2
6419
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
1838
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 it's a Sharepoint WebPart. I'm trying to do something fairly simple, create a datagrid that displays where and when a person works and allows them to change some of the information via DropDownLists. When the user clicks to edit a row, three of...
7
7462
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 data in the grid? It's not in the grid.items collection - only rows there. there is no grid.header or grid.footer. --
0
2732
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, cannot serch by combine 3 table) Example I have the query table below, how do I make the code to seach based on the query from this: SELECT Product.ID, Product.Description, Quantity.Quantity, Quantity.SeialNo, Quantity.SupplierID,...
2
6600
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 in the child datagrid and then go to the combobox and choose another vendor. When the new vendor is loaded nothing shows in the datagrid but the itemsource shows the info is there. Know if I click on the child cell and then click back on the...
0
9875
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9715
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10945
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10614
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7859
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7040
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5697
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5886
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3146
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.