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.
Are you setting the DisplayMember and ValueMember properties? Have a look at the DataSource property on MSDN to see how they are setting all three properties.
UPDATE:
Try switching this:
accommodationList.DisplayMember = "textToShow";
// load and set up accommodation
List<AccommodationEntity> relatedAccommodations =
dt.listHolidayAccommodation(relatedHoliday);
accommodationList.DataSource = relatedAccommodations;
to this:
// load and set up accommodation
List<AccommodationEntity> relatedAccommodations =
dt.listHolidayAccommodation(relatedHoliday);
accommodationList.DataSource = relatedAccommodations;
accommodationList.DisplayMember = "textToShow";
Did you try to set the DisplayMember and DisplayValue like this. Where "Name"/"Value" is the name of the property in the Accomodation class.
List<AccommodationEntity> relatedAccommodations = dt.listHolidayAccommodation(relatedHoliday);
accommodationList.DataSource = relatedAccommodations;
accommodationList.DisplayMember = "Name";
accommodationList.ValueMember = "Value";
UPDATE:
Make sure textToShow
is actually a Property, not just a public field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With