Geeks With Blogs
Gabriel Lozano-Morán blog

Today someone came with the following question:

I have a DataSet with a DataTable and I have set the property Caption of the DataColumn "InvNbr" to "Invoice Number". However when I bind my DataSet to the DataGridViewControl it will show the Name property of the DataColumn instead of the Caption and according to the MSDN Library the Caption property should be used to display a friendly-name, no?

He was right about the fact that the Caption property should be used by UI controls for the display of the columns' friendly-names but unfortunately the DataGridView control does not show the friendly names. Instead it will show the column name, which are often cryptic names, as the column header.

The reason is that the DataGridViewControl's data binding mechanism is a very generic one and therefore it will ignore the Caption property of the DataColumns just because it hasn't been designed to do so. Since you can bind a whole range of objects to the DataGridViewControl (as long as they implement IList), the designers of the control need to check for the object being bound and if the object is a DataTable have a different execution path to be able to display the caption. Another solution would be to use a declaritive (attribute-based) approach and use reflection during databinding to get the friendly-name but this solution off course comes with a perf penalty.

The designers propably didn't have enough time to implement this and my guess is that they have decided to move this feature further down their product backlog. So in order to display the column's friendly-names rather than the column names one will need to loop through the columns collection of the DataGridView and set the caption accordingly.

Microsoft, how about it?


Cross-posted from The .NET Aficionado Posted on Monday, October 9, 2006 4:18 PM How to , .NET Framework | Back to top


Comments on this post: DataGridView control does not show the column's friendly names

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Gabriel Lozano-Morán | Powered by: GeeksWithBlogs.net