Tuesday, July 13, 2010

Changing GridView Column Headers

If you are wondering a way to change the column header appearing in the .Net GridView control in run time then this post will help you to get it done.

For example think that you need to change the normal grid headers shown in Screen A into something like shown in Screen B.

Screen A

Screen B

I have removed few columns from the grid header and then I did add few customized column headers. The added “Edit” column and “Id” column are spanning to 2 rows, the “Temp Columns” column is spanning to 2 columns.

I think the code is self explanatory. This way you will be able to create complex gridview headers.





  1. protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  2. {
  3.      if (e.Row.RowType == DataControlRowType.Header)
  4.      {
  5.          // Remving the first two colummn headers.
  6.          e.Row.Cells.RemoveAt(0);
  7.          e.Row.Cells.RemoveAt(0);
  8.          // Creating the gridview row object.
  9.          GridViewRow headerGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Selected);
  10.          headerGridRow.ID = "hdrGridRow";
  11.          TableCell HeaderCell;
  12.          // Creating and adding the first header cell.
  13.          HeaderCell = new TableCell();
  14.          HeaderCell.Text = "Edit";
  15.          HeaderCell.ID = "cellEdit";
  16.          HeaderCell.Font.Bold = false;
  17.          HeaderCell.HorizontalAlign = HorizontalAlign.Center;
  18.          HeaderCell.RowSpan = 2; // Spans across 2 rows.
  19.          HeaderCell.ColumnSpan = 1;
  20.          HeaderCell.BackColor = System.Drawing.Color.LightGray;
  21.          HeaderCell.BorderColor = System.Drawing.Color.White;
  22.          HeaderCell.ForeColor = System.Drawing.Color.Red;
  23.          headerGridRow.Cells.Add(HeaderCell);
  24.          // Creating and adding the second header cell.
  25.          HeaderCell = new TableCell();
  26.          HeaderCell.Text = "Id";
  27.          HeaderCell.ID = "cellId";
  28.          HeaderCell.Font.Bold = false;
  29.          HeaderCell.HorizontalAlign = HorizontalAlign.Center;
  30.          HeaderCell.RowSpan = 2;
  31.          HeaderCell.ColumnSpan = 1;
  32.          HeaderCell.BackColor = System.Drawing.Color.LightGray;
  33.          HeaderCell.BorderColor = System.Drawing.Color.White;
  34.          HeaderCell.ForeColor = System.Drawing.Color.Red;
  35.          headerGridRow.Cells.Add(HeaderCell);
  36.          // Creating and adding the third header cell.
  37.          HeaderCell = new TableCell();
  38.          HeaderCell.Text = "Temp Columns";
  39.          HeaderCell.ID = "cellTempColumns";
  40.          HeaderCell.Font.Bold = false;
  41.          HeaderCell.HorizontalAlign = HorizontalAlign.Center;
  42.          HeaderCell.RowSpan = 1;
  43.          HeaderCell.ColumnSpan = 2; // Spans across 2 columns.
  44.          HeaderCell.BackColor = System.Drawing.Color.LightGray;
  45.          HeaderCell.BorderColor = System.Drawing.Color.White;
  46.          HeaderCell.ForeColor = System.Drawing.Color.Red;
  47.          headerGridRow.Cells.Add(HeaderCell);
  48.          // Adding the header row to the gridview.
  49.          GridView1.Controls[0].Controls.AddAt(0, headerGridRow);
  50.      }
  51. }




38 comments:

Anonymous said...

The Lyford Cay School serves as a community resource in favour of supranational and Bahamian families. Lyford Cay Grammar offers a acme law Nursery past High Creed education. To the core a multi-cultural territory that promotes understanding amongst diverse nationalities, students are provided with the opportunities and resources to become cross-cultural learners and guilty citizens. Lyford Cay Seminary is an distinct, non-denominational, time school. The school is a non-profit-making metaphysics ens governed near a Directorship of Directors.

http://katobuhoba.wordpress.com
http://ineqadune.wordpress.com/
http://umatirybul.wordpress.com/
http://eratenojoj.wordpress.com/

Anonymous said...

The Lyford Cay Sect serves as a community resource in favour of ecumenical and Bahamian families. Lyford Cay Grammar offers a exorbitant law Nursery through Treble School education. Through a multi-cultural habitat that promotes reason amongst mixed nationalities, students are provided with the opportunities and resources to develop cross-cultural learners and guilty citizens. Lyford Cay Seminary is an independent, non-denominational, lifetime school. The high school is a non-profit-making metaphysics ens governed around a Directorship of Directors.

http://isalul.wordpress.com/
http://agoxihek.wordpress.com
http://jiwywyjar.wordpress.com
http://osibyl.wordpress.com/

Anonymous said...

The Lyford Cay Denomination serves as a community resource after intercontinental and Bahamian families. Lyford Cay Grammar offers a exorbitant standard Nursery through Luxurious Creed education. To the core a multi-cultural habitat that promotes perception amongst diverse nationalities, students are provided with the opportunities and resources to ripen into cross-cultural learners and front-office citizens. Lyford Cay School is an independent, non-denominational, hour school. The university is a non-profit-making metaphysics ens governed before a Board of Directors.

http://alulamyk.wordpress.com
http://emahycori.wordpress.com/
http://abykatuguw.wordpress.com/
http://icovarobu.wordpress.com/

Anonymous said...

The Lyford Cay Denomination serves as a community resource in favour of international and Bahamian families. Lyford Cay School offers a high standard Nursery through Treble Inculcate education. By a multi-cultural territory that promotes reconciliation amongst multiform nationalities, students are provided with the opportunities and resources to become cross-cultural learners and front-office citizens. Lyford Cay Seminary is an independent, non-denominational, time school. The university is a non-profit-making object governed near a Board of Directors.

http://womobukynu.wordpress.com/
http://jiwywyjar.wordpress.com
http://gapopimaxu.wordpress.com
http://agoxihek.wordpress.com

Anonymous said...

The Lyford Cay School serves as a community resource as far as something intercontinental and Bahamian families. Lyford Cay College offers a enormous par Nursery past Luxurious Creed education. Help of a multi-cultural environment that promotes reason amongst distinctive nationalities, students are provided with the opportunities and resources to ripen into cross-cultural learners and responsible citizens. Lyford Cay Seminary is an competent, non-denominational, time school. The coterie is a non-profit-making entity governed by a Board of Directors.

http://ikycynu.wordpress.com/
http://abykatuguw.wordpress.com/
http://gapopimaxu.wordpress.com
http://eratenojoj.wordpress.com/

Anonymous said...

The Lyford Cay Sect serves as a community resource after intercontinental and Bahamian families. Lyford Cay University offers a exorbitant law Nursery through Piercing Inculcate education. To the core a multi-cultural habitat that promotes perception amongst mixed nationalities, students are provided with the opportunities and resources to develop cross-cultural learners and culpable citizens. Lyford Cay Clique is an independent, non-denominational, lifetime school. The school is a non-profit-making entity governed near a Board of Directors.

http://icovarobu.wordpress.com/
http://womobukynu.wordpress.com/
http://hymabo.wordpress.com
http://ebohyroxy.wordpress.com/

Anonymous said...

The Lyford Cay Sect serves as a community resource after supranational and Bahamian families. Lyford Cay School offers a high standard Nursery entirely High Alma mater education. Help of a multi-cultural circumstances that promotes understanding amongst multiform nationalities, students are provided with the opportunities and resources to develop cross-cultural learners and front-office citizens. Lyford Cay Clique is an independent, non-denominational, time school. The school is a non-profit-making thing governed near a Meals of Directors.

http://eratenojoj.wordpress.com/
http://syzefudyv.wordpress.com
http://ecilili.wordpress.com
http://acixohotyp.wordpress.com

Anonymous said...

The author is really cool. But some of the commentators are just posting stupid words.

Anonymous said...

Hey all
can anybody advise me?
I want a free MMORPG with a massive amount players and so they (perfectly the majority of them) can speak English. It's fundamental that the servers have a very number of players. I'm hunting for something like Diablo and World of warcraft. I am additionally not willing to give any money as I only need some time to destroy at this time. It's ok if the particular game is free of charge along with premium content. I have looked a lot regarding MMORPGs but probably none from the web pages definitely say simply how much people are usually online.
appreciation

Anonymous said...

спасибо было интересно читать

Anonymous said...

Interestingly written! [b][url=http://IPODTOUCH5.INFO/Office-Space.html]Office Space[/url][/b]

Arjuna said...

Thank you all for the appreciations done.

Anonymous said...

Wow, I love these… thanks for sharing

R, A
check my site

Anonymous said...

Simply awesome. And here I thought my generation was the first to master dry wit. Seems I was wrong. ;)

R, Aaron
here's my site too

Anonymous said...

In most countries, such as the United Kingdom, the distinction between a tramway and a railway is precise and defined in law. In the US and Canada such street railways are mostly referred to as trolleys. The key difference between a railroad and a trolley system is the latter running primarily on public streets, whereas trains have a right of way separated from the public. Often the US style interurban and modern light rail are confused with a trolley system, as it too may run on on the street for short or medium long sections. In some languages the word tram also refers to interurban and light rail -style networks, in particular Dutch.
"I didn't really notice what happened as I was sleeping on the train and thought at first that we were just making a scheduled stop," she told QMI.
There are also many other types of wagons, such as "low loader" wagons for transporting road vehicles. There are refrigerator cars for transporting foods such as ice cream. There are simple types of open-topped wagons for transporting minerals and bulk material such as coal, and tankers for transporting liquids and gases. Today, however, most coal and aggregates are moved in hopper wagons that can be filled and discharged rapidly, to enable efficient handling of the materials.
Named trains
Main article: Freight rail transport

[url=http://BESTRAILTRAIN.INFO/article.php?article=873483]Chiltern trains[/url]

Anonymous said...

great blog If you are the type to update your blog regulary, then you have gained one daily reader in me today. keep up the super work.

Anonymous said...

I rattling thankful to find this internet site on bing, just what I was looking for : D likewise saved to fav.

Anonymous said...

Howdy this theme is really concerning. Keep it going friend !

Anonymous said...

Hey this theme is extremely concerning. Keep it going teammate !

Anonymous said...

I have tried many other services but yours appeared to be the best. Lots of thanks.


[url=http://bellmultimedia.com/buying-good-quality-essay-at-samedayessay-com]buy essay from samedayessay.com[/url]

Anonymous said...

I am lucky that I detected this web site, exactly the right info that I was searching for! .

Anonymous said...

I really liked the article, and the very cool blog

Anonymous said...

del plz erorr

Anonymous said...

thanks, very good =)

Anonymous said...

Usually I do not learn post on blogs, however I wish to say that this write-up very pressured me to try and do so! Your writing style has been surprised me. Thank you, very great post.

Anonymous said...

You have brought up a very excellent points, thankyou for the post.

Anonymous said...

I just my opinion, and I am not trying to be critical at all. If you do follow this up then I would be interested. Thanks.

Anonymous said...

Are you looking for a new brand of vehicle? [url=http://quotesauto.org]Autoquotes[/url] - it should allow you to obtain a vehicle and protection that corresponds your requirements.

Anonymous said...

Молодежная рок-группа “Ранетки” говорит вам спасибо, за такой прекрасный блог!

Anonymous said...

One of the more impressive blogs Ive seen.

Anonymous said...


Of course, what a magnificent site and revealing posts, I surely will bookmark your site.Best Regards!

Anonymous said...


Wow, amazing weblog format! How lengthy have you been running a blog for? you make blogging glance easy.

Arjuna said...

Thank you for all the appreciations.

Anonymous said...


I have been surfing online greater than 3 hours as of late, yet I never discovered any fascinating article like yours.

Anonymous said...


Heya i am for the first time here.

Anonymous said...


I was profoundly happy to notice this stuff.I wanted to salute you for your time for this awe-inspiring read:)!!

Anonymous said...


Thanks for the post. I have always seen that almost all people are wanting to lose weight since they wish to look slim and also attractive.

Anonymous said...

It looks very professional. Many of us desire to see what is necessary to know such threads in few month. I found no mention about fresh solutions, such as the auto and home owners insurance US policies. It would be better to get more savings on compare auto rates by zip.