gpt4 book ai didi

c# - 使用特殊类的多个 DisplayMember

转载 作者:太空狗 更新时间:2023-10-29 23:01:54 25 4
gpt4 key购买 nike

我正在尝试获取一个 ListBox 来显示 Accommodation 表的多行的串联。

因为我无法编辑数据源,所以我准备了一个类 AccommodationEntity,它包含原始的 Accommodation 对象和我希望 ListBox 显示的字符串。

但是,由于某些原因,我未能设置 ListBox 的 DisplayMember 属性,因此显示默认的 jibber-jabber。

我按如下方式设置列表框:

accommodationList.DisplayMember = "textToShow";
// load and set up accommodation
List<AccommodationEntity> relatedAccommodations =
dt.listHolidayAccommodation(relatedHoliday);
accommodationList.DataSource = relatedAccommodations;
accommodationList.Refresh();

存储在数据源中的对象类如下所示:

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 + ")";
}
}

}`

最后,有一个方法可以进行一些搜索(返回正确的对象):

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;
}

我知道数据源包含正确的数据,但出于某种原因,DisplayMember 属性似乎仍设置为 ""

如有任何帮助,我们将不胜感激。

谢谢。

最佳答案

您是否正在设置 DisplayMemberValueMember特性?看看 DataSource MSDN 上的属性以查看他们如何设置所有三个属性。

更新:

尝试切换这个:

accommodationList.DisplayMember = "textToShow";
// load and set up accommodation
List<AccommodationEntity> relatedAccommodations =
dt.listHolidayAccommodation(relatedHoliday);
accommodationList.DataSource = relatedAccommodations;

为此:

// load and set up accommodation
List<AccommodationEntity> relatedAccommodations =
dt.listHolidayAccommodation(relatedHoliday);
accommodationList.DataSource = relatedAccommodations;
accommodationList.DisplayMember = "textToShow";

关于c# - 使用特殊类的多个 DisplayMember,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com