I am trying to get a ListBox to display a concatenation of multiple rows of table Accommodation.
Because I can’t edit the datasource, I prepared a class, AccommodationEntity, which contains both the original Accommodation object and the string I want the ListBox to display.
However, for some reason, I fail to set the DisplayMember property of the ListBox, which thus displays the default jibber-jabber.
I set up the listbox as follows:
accommodationList.DisplayMember = "textToShow";
// load and set up accommodation
List<AccommodationEntity> relatedAccommodations =
dt.listHolidayAccommodation(relatedHoliday);
accommodationList.DataSource = relatedAccommodations;
accommodationList.Refresh();
The class for the objects stored in the datasource looks like this:
class AccommodationEntity
{
public accommodation classicAccommodation;
public string textToShow;
public AccommodationEntity(stay relatedStay)
{
this.classicAccommodation = relatedStay.accommodation;
string from = relatedStay.dateFrom.ToString();
string to = relatedStay.dateTo.ToString();
string city = relatedStay.accommodation.location.ToString();
string hotelName = relatedStay.accommodation.name.ToString();
this.textToShow = hotelName + ", " + city + " (" + from + " - " + to + ")";
}
}
}
`
And finally, there is a method which does some searching (returning correct objects):
public List<AccommodationEntity> listHolidayAccommodation(holiday selectedHoliday)
{
List<AccommodationEntity> ubytovani = new List<AccommodationEntity>();
var stays = from singleStay in selectedHoliday.stays
select singleStay;
foreach (stay singleStay in stays)
{
AccommodationEntity newStay = new AccommodationEntity(singleStay);
ubytovani.Add(newStay);
}
return ubytovani;
}
I know the dataSource contains the right data, but for some reason, it seems the DisplayMember property remains set to "".
Any help would be appreciated.
Thanks.
Did you try to set the DisplayMember and DisplayValue like this. Where “Name”/”Value” is the name of the property in the Accomodation class.
UPDATE:
Make sure
textToShowis actually a Property, not just a public field.