Monday, March 15, 2010

DevExpress XtraGrid anonymous column

Well I found myself needing to bind a DevExpress XtraGrid to a generic List of strings. I found their ListBox to be rather limited and figured a one-column grid massaged the right way would serve quite well as a faux ListBox.

On my first attempt, I bound the list directly to the grid and found that the text did not wrap. That I could fix by setting the ColumnEdit to a MemoEdit and setting the Options.View.AutoHeight property to True. There was only one catch. At design time, I did not know what FieldName I should use in order to bind to the values in the List. If I were binding to a List of Users, I would specify a FieldName such as UserName or EmailAddress and it would bind just fine. What FieldName do you bind to when you just want to bind to a List of strings? "Column". Yes, just "Column".

I discovered this arbitrary value by getting the FieldName from the column at runtime when binding directly to the grid with no columns specified at design time. Hope this helps someone.

No comments:

Post a Comment