[英]Subset List of parent List not binding to Listbox in Winforms
I am trying to bind a List of a custom class to a Listbox and cannot get anything to display. 我正在尝试将自定义类的列表绑定到列表框,并且无法显示任何内容。 The List is a subset of another List.
该列表是另一个列表的子集。 I can bind the parent List and see the items, but not the child List.
我可以绑定父列表并查看项目,但不能绑定子列表。 How can I get the subset List to bind to the Listbox?
如何获取子集列表以绑定到列表框? I have tried changing the order of the ListBox's DisplayMember, ValueMember, and DataSource properties.
我尝试更改ListBox的DisplayMember,ValueMember和DataSource属性的顺序。 In debugging I can see that the DataSource has the correct values, but I can't get them to display.
在调试中,我可以看到DataSource具有正确的值,但无法显示它们。 Relevant code below:
相关代码如下:
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 containing global List collection: 包含全局列表集合的GlobalVariables类:
public static List<DimZone>[] zoneCollection = new List<DimZone>[maxServerCount];
Form using global List collection and subset 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;
}
}
Your code contains various unclear parts. 您的代码包含各种不清楚的部分。 In any case, the best proceeding in these situations is setting up a properly-working simpler code and modifying it until reaching the stage you want.
无论如何,在这些情况下,最好的做法是设置一个工作正常的简单代码并对其进行修改,直到达到您想要的阶段为止。 I can provide this properly-working first step.
我可以提供此正常工作的第一步。 Sample code:
样例代码:
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; }
}
Try this code and confirm that the changes in zoneListBox.DisplayMember
(eg, "Zone_Key"
, "Zone_ID"
, etc.) are immediately reflected in the values being displayed by zoneListBox
. 尝试使用此代码,并确认
zoneListBox.DisplayMember
中的更改(例如"Zone_Key"
, "Zone_ID"
等)会立即反映在zoneListBox
显示的值中。
The problem was I was changing zoneListBox.DataSource
from one source to another on load, which caused the error. 问题是我在加载时将
zoneListBox.DataSource
从一个源更改为另一个,这导致了错误。 In order for the DataSource to update properly, I had to set zoneListBox.DataSource = null
before updating to a new DataSource. 为了正确更新数据源,在更新到新的数据源之前,我必须设置
zoneListBox.DataSource = null
。 I don't know why I have to set it to null first, but it solved my problem. 我不知道为什么必须先将其设置为null,但这解决了我的问题。 So my updated code is as follows:
所以我的更新代码如下:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.