Scott Cate Weblog 

scott.cate@myKB.com
http://scottcate.myKB.com



Scott Cate KB > ASP.NET Code Samples - Findings > DataGrid Control Start Over
Search the Knowledge Base
 
Start Search in the Following Category
Date Modified
Monday, December 23, 2002
Adding Header Row to a Datagrid

This seems to be a common question and I had to deal with it just yesterday. After a little while I found the solution and thought I'd share it.

It's a great trick to add a header description, or a toolbar control, or whatever you need at the top of your datagrid. It could just as easily be used as a footer trick. The trick here is knowing that DataGridItem is actually a row in the DataGrid.

Once I found this object, it was all downhill. So we create a row (DataGridItem), then create a cell, span the colomncount, and set the text, so now I have a "Header Row" that I can assign a text property to, and it's a nice look. Add the cell to the DataGridItem, and finally add the DataGridItem to the DataGrid.

I'm actually going a step farther, and adding a toolbarTable to the new cell (which of course is the header row)

Here's the skinny. (Watch for code wrapping)

private void DataGrid_ItemDataBound(object source, DataGridItemEventArgs eventArguments)
{
  TableCellCollection __Cells = eventArguments.Item.Cells;
int __CellsCount = eventArguments.Item.Cells.Count;
switch (eventArguments.Item.ItemType)
{
    case ListItemType.AlternatingItem :
case ListItemType.Item :
case ListItemType.Footer :
case ListItemType.Header :
if (this.Header!=String.Empty)
{
     

TableCell __CellHeader = new TableCell();
__CellHeader.Text = this.Header;
__CellHeader.ColumnSpan = __CellsCount;

DataGridItem __DataGridItemHeader = new DataGridItem (0,0,ListItemType.Header);
__DataGridItemHeader.Cells.Add(__CellHeader);
_DataGrid.Controls[0].Controls.AddAt(0,__DataGridItemHeader);

    }//if
break;
  }//switch
}//DataGrid_ItemDataBound(object,DataGridItemEventArgs)
Article References
Join my DataGrid discussion on Yahoo


All rights reserved. All details are the personal opinion of Scott Cate.
All trademarks referenced are the property of their respective owners.
Scott Cate is a lead programmer for www.myKB.com and
owner of The Arizona .NET User Group and an all around nice guy ;)


Knowledge Base Software - myKB.com

 

Cameron Cate Pictures
Cameron Cate Pix

Site Navigation

Home
Knowledge Base
Wiki Discussions
Photo Album

Sites I Own & Run


Easy Search ASP.NET
mykb.com knowledge base software logo
myKB.com
Knowledgebase Software

KBAlertz.com
Arizona .NET user Group
Group Leader

Affiliations


ASP.NET MVP
2004
2005
2006
2007
2008
2009


Blog Sites I Read

Brady Gaster
Rob Howard
G. Andrew  Duthie
Robert McLaws
Alex Lowe

World of Scott.Net

Scott Guthrie
  -  Father of ASP.NET

Scott Watermasyk
  - .Text (Weblog)

Scott Sargent
Scott Mitchell
ScottG.net
Scott Bellware
Scott Forsyth
Scott Hanselman
Scott Cate

Favorite Books


ASP.NET Cookbook


First Looks @ ASP.NET 2.0