Named DataGrid columns – wouldn’t it be nice? – level 300

Earlier, I blogged about using controls that are embedded in unusual places in the control tree of the DataGrid.  For instance, putting a LinkButton for adding a new record in the header or footer of the DataGrid.  You have to manually manipulate the Controls collection to do this. 


Another issue is what if you put that LinkButton in the same column as the Edit, update, cancel, delete buttons?  If you want to restrict editing of the data, a step on the UI would be to hide that entire column.  Seems pretty straight-forward, but how do you get a reference to that column?  I searched, and the only way I found was to reference it by the index in the Columns collection:


dgd.Columns[7].Visible = false;


That works well, but it bit me in the butt when I tried to maintain that code.  Down the road it became necessary to add two columns to that DataGrid.  Then, you guessed it!  A nice, little bug in my code.  Usually I’m able to avoid the debugger in VS by writing modular code and using the heck out of the ASP.NET Trace feature (great for code maintenance), but this was an issue that actually required a breakpoint.  Once inside the code, it was very clear what was happening.  I changed the index to the new index of the edit column, and my problem was fixed (until next time), but isn’t there a more maintainable way to do this?  What happens when another developer needs to modify my application.  Yeah, I document the heck out of it, but why can’t the Columns collection support column name identifiers as well.  I know the answer (CollectionBase), but it would be nice.


For now, I’ll have several lines of comments above that code.