父列表的子集列表未绑定到 Winforms 中的列表框
Subset List of parent List not binding to Listbox in Winforms
我正在尝试将自定义 class 的列表绑定到列表框,但无法显示任何内容。该列表是另一个列表的子集。我可以绑定父列表并查看项目,但不能绑定子列表。如何让子集列表绑定到列表框?我尝试过更改 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; }
}
GlobalVariables Class 包含全局列表集合:
public static List<DimZone>[] zoneCollection = new List<DimZone>[maxServerCount];
使用全局列表集合和子列表的表单:
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;
}
}
您的代码包含各种不清楚的部分。在任何情况下,在这些情况下最好的做法是设置一个正常工作的更简单的代码并修改它直到达到你想要的阶段。我可以提供这个正常工作的第一步。示例代码:
private void Form1_Load(object sender, EventArgs e)
{
List<DimZone> source = new List<DimZone>();
DimZone curZone = new DimZone() { Zone_Key = 1, Zone_ID = 11, Facility_Key = 111, Zone_Name = "1111", Zone_Type = "11111" };
source.Add(curZone);
curZone = new DimZone() { Zone_Key = 2, Zone_ID = 22, Facility_Key = 222, Zone_Name = "2222", Zone_Type = "22222" };
source.Add(curZone);
zoneListBox.DisplayMember = "Facility_Key";
zoneListBox.DataSource = source;
}
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; }
}
尝试此代码并确认 zoneListBox.DisplayMember
中的更改(例如 "Zone_Key"
、"Zone_ID"
等)会立即反映在 [=14= 显示的值中].
问题是我在加载时将 zoneListBox.DataSource
从一个来源更改为另一个来源,这导致了错误。为了让 DataSource 正确更新,我必须在更新到新的 DataSource 之前设置 zoneListBox.DataSource = null
。我不知道为什么我必须先将它设置为 null,但它解决了我的问题。所以我更新后的代码如下:
private void PopulateUI()
{
if (zoneCollectionAppended != null)
{
zoneListBox.DisplayMember = "Zone_Name";
zoneListBox.ValueMember = "Zone_ID";
//DataSource had to be reset to null before updating to new DataSource
zoneListBox.DataSource = null;
zoneListBox.DataSource = zoneCollectionAppended;
}
}
我正在尝试将自定义 class 的列表绑定到列表框,但无法显示任何内容。该列表是另一个列表的子集。我可以绑定父列表并查看项目,但不能绑定子列表。如何让子集列表绑定到列表框?我尝试过更改 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; }
}
GlobalVariables Class 包含全局列表集合:
public static List<DimZone>[] zoneCollection = new List<DimZone>[maxServerCount];
使用全局列表集合和子列表的表单:
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;
}
}
您的代码包含各种不清楚的部分。在任何情况下,在这些情况下最好的做法是设置一个正常工作的更简单的代码并修改它直到达到你想要的阶段。我可以提供这个正常工作的第一步。示例代码:
private void Form1_Load(object sender, EventArgs e)
{
List<DimZone> source = new List<DimZone>();
DimZone curZone = new DimZone() { Zone_Key = 1, Zone_ID = 11, Facility_Key = 111, Zone_Name = "1111", Zone_Type = "11111" };
source.Add(curZone);
curZone = new DimZone() { Zone_Key = 2, Zone_ID = 22, Facility_Key = 222, Zone_Name = "2222", Zone_Type = "22222" };
source.Add(curZone);
zoneListBox.DisplayMember = "Facility_Key";
zoneListBox.DataSource = source;
}
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; }
}
尝试此代码并确认 zoneListBox.DisplayMember
中的更改(例如 "Zone_Key"
、"Zone_ID"
等)会立即反映在 [=14= 显示的值中].
问题是我在加载时将 zoneListBox.DataSource
从一个来源更改为另一个来源,这导致了错误。为了让 DataSource 正确更新,我必须在更新到新的 DataSource 之前设置 zoneListBox.DataSource = null
。我不知道为什么我必须先将它设置为 null,但它解决了我的问题。所以我更新后的代码如下:
private void PopulateUI()
{
if (zoneCollectionAppended != null)
{
zoneListBox.DisplayMember = "Zone_Name";
zoneListBox.ValueMember = "Zone_ID";
//DataSource had to be reset to null before updating to new DataSource
zoneListBox.DataSource = null;
zoneListBox.DataSource = zoneCollectionAppended;
}
}