C# 加载 ListBox 的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/303248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the proper way to load up a ListBox?
提问by BuddyJoe
What is the proper way to load a ListBox
in C# .NET 2.0 Winforms?
ListBox
在 C# .NET 2.0 Winforms 中加载 a 的正确方法是什么?
I thought I could just bind it to a DataTable
. No such luck.
I thought I could bind it with a Dictionary
. No luck.
我以为我可以将它绑定到DataTable
. 没有这样的运气。
我以为我可以用Dictionary
. 没运气。
Do I have to write an class called KeyValuePair
, and then use List<KeyValuePair>
just to be able to load this thing with objects? Maybe I am missing something obvious. I want my display text and values to be different values.
我是否必须编写一个名为 的类KeyValuePair
,然后使用List<KeyValuePair>
它才能用对象加载这个东西?也许我错过了一些明显的东西。我希望我的显示文本和值是不同的值。
采纳答案by BFree
Simple code example. Say you have a Person
class with 3 properties. FirstName
, LastName
and Age
. Say you want to bind your listbox to a collection of Person
objects. You want the display to show the first name, but the value to be the age. Here's how you would do it:
简单的代码示例。假设您有一个Person
具有 3 个属性的类。FirstName
,LastName
和Age
。假设您想将列表框绑定到一组Person
对象。您希望显示器显示名字,但值是年龄。以下是您的操作方法:
List<Person> people = new List<Person>();
people.Add(new Person { Age = 25, FirstName = "Alex", LastName = "Johnson" });
people.Add(new Person { Age = 23, FirstName = "Hyman", LastName = "Jones" });
people.Add(new Person { Age = 35, FirstName = "Mike", LastName = "Williams" });
people.Add(new Person { Age = 25, FirstName = "Gill", LastName = "Hymanson" });
this.listBox1.DataSource = people;
this.listBox1.DisplayMember = "FirstName";
this.listBox1.ValueMember = "Age";
The trick is the DisplayMember
, and the ValueMember
.
诀窍是DisplayMember
, 和ValueMember
。
回答by Jason Sundram
Lets assume your data type is called MyDataType. Implement ToString() on that datatype to determine the display text. e.g.:
让我们假设您的数据类型称为 MyDataType。在该数据类型上实现 ToString() 以确定显示文本。例如:
class MyDataType
{
public string ToString()
{
//return the text you want to display
}
}
Then you can take a list consisting of your datatype and cram it into the ListBox via AddRange() as follows:
然后,您可以获取一个由您的数据类型组成的列表,并通过 AddRange() 将其填充到 ListBox 中,如下所示:
ListBox l;
List<MyDataType> myItems = new List<MyDataType>(); // populate this however you like
l.AddRange(myItems.ToArray());
Let me know if you need more help - it would help to know what datatype you are trying to display in the listbox.
如果您需要更多帮助,请告诉我 - 了解您尝试在列表框中显示的数据类型会有所帮助。
回答by JamesSugrue
You can set the datasource to whatever datasource you like that implements the IList or IListSource.
您可以将数据源设置为您喜欢的任何实现 IList 或 IListSource 的数据源。
You will also need to set the DisplayMember and the ValueMember properties to the fields you want to display and have the values associated with respectively.
您还需要将 DisplayMember 和 ValueMember 属性设置为要显示的字段,并分别具有关联的值。
回答by Aaron Palmer
You can bind a DataTabledirectly...
你可以直接绑定一个DataTable...
listbox.ValueMember = "your_id_field";
listbox.DisplayMember = "your_display_field";
listbox.DataSource = dataTable;
回答by Gus Paul
Using the DataSource paramater used to suck performance wise - on ComboBoxes at least,
使用 DataSource 参数用于明智地吸收性能 - 至少在 ComboBoxes 上,
I am now heavily conditioned to override ToString() on the object and just adding the objects using the Items.AddRange() method, as another commenter above describes.
我现在非常习惯在对象上覆盖 ToString() 并只使用 Items.AddRange() 方法添加对象,如上面的另一位评论者所述。
回答by user39369
To bind to a dictionary you have to wrap it in a new BindingSource object.
要绑定到字典,您必须将它包装在一个新的 BindingSource 对象中。
MyListBox.DataSource = New BindingSource(Dict, Nothing)
MyListBox.DisplayMember = "Value"
MyListBox.ValueMember = "Key"