【发布时间】:2015-12-10 16:15:25
【问题描述】:
我正在尝试将自定义类的列表绑定到列表框,但无法显示任何内容。该列表是另一个列表的子集。我可以绑定父列表并查看项目,但不能绑定子列表。如何让子集列表绑定到列表框?我尝试更改 ListBox 的 DisplayMember、ValueMember 和 DataSource 属性的顺序。在调试中,我可以看到 DataSource 具有正确的值,但我无法显示它们。相关代码如下:
public class DimZone
{
public int Zone_Key { get; set; }
public int Zone_ID { get; set; }
public int Facility_Key { get; set; }
public string Zone_Name { get; set; }
public string Zone_Type { get; set; }
}
包含全局 List 集合的 GlobalVariables 类:
public static List<DimZone>[] zoneCollection = new List<DimZone>[maxServerCount];
使用全局 List 集合和子集 List 的表单:
List<DimZone> zoneCollectionAppended = new List<DimZone>();
private void StaffStatusReportForm_Load(object sender, EventArgs e)
{
facilityComboBox.DataSource = GlobalVariables.facilityCollection;
GetFacilityIndex();
CreateZoneAppendedList();
PopulateUI();
}
private void CreateZoneAppendedList()
{
foreach (var zone in GlobalVariables.zoneCollection[currentFacilityIndex])
{
if (zone.Zone_Name != "All")
{
zoneCollectionAppended.Add(zone);
}
}
}
private void PopulateUI()
{
if (zoneCollectionAppended != null)
{
zoneListBox.DisplayMember = "Zone_Name";
zoneListBox.ValueMember = "Zone_ID";
zoneListBox.DataSource = zoneCollectionAppended;
}
}
【问题讨论】:
标签: c# winforms visual-studio-2013 data-binding listbox