473,405 Members | 2,421 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,405 software developers and data experts.

Header Spanning 2 Columns GridView

yoda
291 100+
Hello Everyone,

I've scoured internet looking for a way to span a heading over over 2 columns and to no avail. There are many solutions but none are what I'm looking for.

I've attached a excel file with what the gridview should look like.

If you look there are headers that span 2 columns. The columns with Count and Balance should be under these headers.

All the columns in the table are dynamically bound during run time, meaning I create them in code. So with that in mind.

Is there a simple way of doing this?

I've tried using a Gridview row and then adding tablecells and spanning the cells 2 columns but when you export the file it looks like garbage.

Any help would be appreciated!

Thanks,
Yoda.
Attached Files
File Type: xls GridExample.xls (27.0 KB, 557 views)
Feb 22 '12 #1
5 7609
yoda
291 100+
Figured it out. Ended up just adding another header row to the to the GridView and adding TableCells to it and calling the TableCell.ColumnSpan property and setting it to 2.

Example:

Expand|Select|Wrap|Line Numbers
  1. //Create a new Header Row
  2. GridViewRow HeaderGridRow =
  3.             new GridViewRow(0, 0, DataControlRowType.Header,DataControlRowState.Normal);
  4.  
  5. //Create TableCells
  6. TableCell HeaderCell = new TableCell();
  7. HeaderCell.Text = text;
  8. HeaderCell.ColumnSpan = colspan;
  9. HeaderGridRow.Cells.Add(HeaderCell);
  10.  
  11. //Add new Header Row to Gridview
  12. GridView1.Controls[0].Controls.AddAt
  13.             (0, HeaderGridRow);
  14.  
Feb 23 '12 #2
Frinavale
9,735 Expert Mod 8TB
I would have approached this a lot differently than you have.

First of all, I would never use Response.Write in my C# or VB.NET code behind because the location of where the writes take place will likely be out side of the HTML <body> tag. This will make your page's HTML invalid.

Instead I would use ASP.NET controls that I could access in my code behind to set the styles for the controls.

That's kind of besides the point.

The other thing that I would do a lot differently is create a TemplateField for my columns within the GridView. If you need to have dynamic columns (ie: the number of columns in your GridView is going to change) then I would create a TemplateField class to use in my dynamic code (this is pretty advanced though).

So, to keep things simple, in the ASP mark up (not the C# or VB.NET code behind) I would define template fields for my columns and bind the controls within these templates to the data that I am expecting to be binding to the GridView.

This way I could display 2 pieces of bound information in one column.

-Frinny
Feb 24 '12 #3
yoda
291 100+
Unfortunately this program hasn't been developed by me, and hasn't been built from the ground up. So I can't really say how valid or invalid the HTML is and I'm going of off what was already there and google. I've never programmed in ASP.NET or C#(Though it's very similar to Java) so this is all new to me, and haven't figured out the limitations of ASP since I've rarely programmed for the web.

But thank you for the insight on how you would do it. I'm only been programming for the last 2 years or so as a Computer Programmer Analyst student. So I have quite a bit to learn yet and haven't really narrowed myself down to specific field of study.

But thank you again. Also if you don't mind me asking how would you of done exporting of a GridView then in ASP.NET 2.0 with all the colours and such stylings?

Thanks,
Yoda.
Feb 24 '12 #4
Frinavale
9,735 Expert Mod 8TB
Ok, I think I'm confused about what your problem is.

Are you having a problem exporting the data in a particular format?

Or are you having a problem displaying the data in web browser within the GridView?

I haven't had the pleasure of exporting to excel but I have exported data to a .cvs format. I took the datasource for the GridView and parsed it into Comma-Separated-Values (following the rules on what the format should be to create a valid csv file).

Excel would probably require the same thing but I would be creating a Microsoft Excel file object and putting the data from the DataSource into that instead.

-Frinny

PS I cannot open your excel file with the netwrok settings that are imposed on me.

Also, if you are exporting, it's perfectly valid to use the response.write since you aren't sending HTML back to the browser.
Feb 24 '12 #5
yoda
291 100+
Yea sorry I add how I export my GridView to an excel for some reason, but non in particular. I should probably edit that post to take that out.

Yea I originally I had trouble with my GridView viewing the columns properly here's the format with out the excel:

Col 1 ---- Col 2 ----- Col 3 -------------A---------------B--------------C--------------D------
|--Col1--||--Col 2--||--Col 3--|----|| Cnt | Bal ||--|| Cnt | Bal ||--|| Cnt | Bal ||--|| Cnt | Bal ||


The first three header columns only have one bound column, where A to D all have 2 sub columns that are data bound Count and Balance. I had trouble originally getting the header columns to span over 2 columns, so my solution was to make the count and balance header columns, then add another header row on top of the gridview and then make table cells and add them to the new header row and make that cell span 2 columns using the TableCell.ColumnSpan property.


Yea I've created a csv before and even had the challenge of zipping a csv while it's still on the server before it hits the client side for download, that was interesting.

Thanks for the info!
Yoda.
Feb 24 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Matt Kruse | last post by:
I'm looking for the best JS/CSS solution to add functionality to tables. The only browser which needs to be supported is IE5.5+, but no activeX can be used. to be able to do: - Fixed header row...
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...
1
by: Cristof Falk | last post by:
Is there an easy way to have a scrollable grid where you can lcok top lines or left columns for headings? It seems like you have to purchase this, which we'd rather not do. We're most interested in...
1
by: ZeroDev | last post by:
how can i do a multi header asp.net gridview and if it is Posible to do it as a class to use it any time , i mean Generic way , so ic an include the Header count style etc ,
4
by: tirath | last post by:
hi, I want to display data in on my web page, in gridview in this format: Group A Group B A1 A2 B1 B2 Test 1 1 2 1 2 Test 2 1 2 1 2 Group A is header to columns A1 and A2...
2
by: =?Utf-8?B?R2lyaXNo?= | last post by:
Hello All, I am displaying a report in a gridview and also in a separte html table. When the user clicks a print button, I print the report (from gridview or from html table). But I want to repeat...
1
by: =?Utf-8?B?Um9iZXJ0IFNtaXRo?= | last post by:
Hello, I have a gridview bound to a datasource, however there are many rows in the grid and the user has to scroll down and thus the header row is obscurred. Is there anyway of keeping these...
1
by: ASPnewb1 | last post by:
this is used when the Updating Row is fired the first line works in grabbing the text from the textbox of the cell, but I cannot access the column header text, as it's telling me the columns do...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.