It is a common scenario to put a DropDownList into GridView's or DetailsView's Item Template. Since most of the time we use object-oriented architecture, the DropDownList's data source is a business method which returns list of business entities.
Long ago, when we've faced this scenario for the first time, we had to solve the formatting issue - when binding a list to the DropDownList, you specify DataTextField and DataValueField:
Issues occur when you need the items to be formatted in a custom way.
The most convenient and coherent method of adding custom formatting is to add a dummy property to the business object class:
so that you can force custom formatting of your business objects inside the DropDownList like this (note the This value for the DataTextField property) :
This worked like a charm for us, well until few days ago when we've tried the same on business objects persisted with DevExpress XPO
and instead of custom formatting I got DevExpress.XPO.XPCollection(14) Count(1), DevExpress.XPO.XPCollection(15) Count(1) ... instead of custom formatted descriptions.
After over 2 hours the battle was won. Following trick
does the job.
It turns out that XPObject inherits from XPCustomObject which inherits from XPBaseObject which has the ToString method defined and this is where the problem lies. Only changing the signature of the This property gives expected results.
The solution is, however, far beyond any explanation I am able to come up with.