Accessible DataGrid Header
An easy way to get a DataGrid control to display a <TH> row? As you probably know, by default the header row shows up using <TD> elements. However, <TH> elements are often required for accessibility purposes. As far as I know, in Whidbey the GridView control--the DataGrid successor--will have an ad hoc property that, if set, enables the control to generate a <TH> header.
How to get the same for ASP.NET 1.x? As Scott Galloway suggested, go here and get this hotfix for ASP.NET 1.1. Or in alternative read on.
I'm afraid you need a super DataGrid class. The following code captures the markup that a certain fully configured DataGrid control would generate.
StringWriter writer = new StringWriter();
HtmlTextWriter buffer = new HtmlTextWriter(writer);
base.Render(buffer);
string gridMarkup = writer.ToString();
The gridMarkup string contains the HTML text for a table. At this point, you parse that text and replaces the header's TD with TH elements. When you're done, you simply output the new string. Where does all thing happen? In the overridden Render method of your super DataGrid control.
protected override void Render(HtmlTextWriter output)
{
// Get the default markup
// TODO
// Modify the string
// TODO
// Write it out
output.Write(gridMarkup);
}
This code is illustrated (with other purposes) in my January 2004 Cutting Edge column on MSDN Magazine.