I’ve been working in .NET 1.1 the past few weeks and this morning I figured out a simple solution to a problem that may not even exist anymore in later versions (I try to stay away from UI work so I’m a bit behind in that area). I created a custom business object that exposes several properties as primitives (string, bool, etc.). What a pleasant surprise to find that binding my object to a DataGrid required nothing more than setting the grid’s DataSource equal to an instance of my object. No different than binding to a DataSet, which I’ve done many times.

Enter the problem: one of the properties in my object exposes a Collection, and the DataGrid doesn’t know how or what to display in that column. After flailing around in Google for an hour, I found a clue in my own code: in the column that is mapped to the collection was the fully-qualified name of the Collection class. Extrapolating from this that the DataGrid is probably calling ToString() on each field to display its contents, I overrode my collection’s ToString() method and iterated the internal collection to build up the output string. And what do you know, sure enough, the data I wanted to see in that column automagically appeared with a recompile and a simple browser refresh.

Granted – this is far from the perfect generic solution for this problem. There is no way to modify the output’s format short of parsing and reassembling it (nasty!). But in following the YAGNI principle I know this can be fixed up later with relatively little pain if it becomes necessary.

Advertisements