473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

datagrid - Header span over multiple columns

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.

Nov 19 '05 #1
1 3411
Hi Sam,

Here's some code that should get you going on this. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

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
' 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 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

<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>

<sr*****@mailcity.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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.


Nov 19 '05 #2

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

Similar topics

7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
6
by: Chuck | last post by:
I have a report with three columns, accross then down, and two groups. Currently the group headers are only one column wide and appear in the left hand column. How can I make the group header be...
8
by: Alejandro Penate-Diaz | last post by:
I have a datagrid inside a table and I want to fix both (table and databrid) width to 700, but when the content of datagrid is too large the width increases automatically sometimes and sometimes...
1
by: Olav Tollefsen | last post by:
Is it possible to create a DataGrid footer to span multiple columns? Olav
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
6
by: tshad | last post by:
I have a datagrid where each column has both a label and a linkbutton. I need to have the label left justified and the linkbutton right justified. In HTML, I would have to set up 2 cells to do...
4
by: Mortar | last post by:
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...
1
by: Dave | last post by:
Hi, I'm trying to use templated columns to format the header for my datagrid. Can I use the HeaderTemplate to make the "My Title Goes.." text to span across the entire datagrid table when it's...
2
by: Mike Baugh | last post by:
I am using visual studio 2005 to develop a form using c# I have 3 datagrids on one form. I can set the row color based on a certain value in a column. However this color applies to all 3...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.