I have a standard ASP.NET GridView and I’d like the first column (a emplateField) to be rendered as <th>, or in ASP.NET terms, I’d like to set it to the GridView RowHeaderColumn property. But that property is looking for the name of a DataItem (from a BoundColumn).
How can I render my TemplateField with <th> tags?
Finally found a workaround for this. I am not sure if this code has anything to do with good ASP.NET practices, but it does the trick:
Let me explain what is going on here. We are creating a special type of
GridView, that will render its first column using<th>tags no matter how this column is created. For this we are overriding theInitializeRowmethod. This method basically configures cells for the row. We are handling the first cell, and let standardGridViewtake care of the rest.The configuration we are applying to the cell is fully taken from the
GridViewimplementation and is enough for the cell to be rendered with<th>tag instead of<td>.After that workaround the usage is absolutely standard – register our class as a server control and use it as usual
GridView: