I am trying to get a ListBox to display the concatenation of several rows of a table Accommodation.
Since I cannot edit the data source, I prepared a class AccommodationEntitythat contains both the source object Accommodationand the row that I want to display the ListBox.
However, for some reason, I cannot set the DisplayMember property in the ListBox, which thus displays jabber-jabber by default.
I set the list as follows:
accommodationList.DisplayMember = "textToShow";
// load and set up accommodation
List<AccommodationEntity> relatedAccommodations =
dt.listHolidayAccommodation(relatedHoliday);
accommodationList.DataSource = relatedAccommodations;
accommodationList.Refresh();
The class for objects stored in the data source is as follows:
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 that performs a search (returns the 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;
}
, dataSource , - , DisplayMember "".
.
.