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