且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用c#将数据从对象列表映射到ComboBox。

更新时间:2023-02-09 12:29:01

我可以从你的问题中收集的是你有一个通用的列表,比如

列表< student> StudentList;< / student>



你需要遍历列表元素,它们是Student对象并绑定它们的属性(Id和在您的情况下命名为控件的Value和Text字段。

使用foreach循环遍历列表。



如果你的意思完全不同,请改进问题。


引用:

foreach(学生在studList中)

{

ComboBox item = new ComboBox();

item.DisplayMember = s.getStudentName();

item.ValueMember = s.getStudentId()。ToString();

cb_StudentName.Items.Add(item);

}



仔细查看代码块。在这里,您将创建一个组合框对象的对象,每次都添加它的属性。

您应该尝试这样的事情:

 ComboBox item = new ComboBox(); 
item.DataSource = studList;
item.DisplayMember =StudentName;
item.ValueMember =StudentID;
//我不确定你的cb_StudentName对象是做什么的。希望它可能是文字或一些占位符。
cb_StudentName.Items.Add(item);



我还建议您阅读以下内容:

MSDN:ComboBox [ ^ ]

如何将列表绑定到组合框? (Winforms) [ ^ ]



     - Amy


item 是ComboBox类的一个实例。因此它是一种控制。它不是特定ComboBox实例的列表项。



如果您只想添加列表项,则无需创建ComboBox对象。同样的逻辑也适用于任何控件。



你可以试试这样的东西。



列表studList = dm.getStudList(); 

foreach (学生的 in studList)
{
ListItem item = new ListItem();
item.DisplayMember = s.getStudentName();
item.ValueMember = s.getStudentId()。ToString();
cb_StudentName.Items.Add(item);
}


Hii

Scenario:

I am developing windows form application using C# and MySQL.
I am fetching data from database and putting it in list. And returning list to another class.
Now I want to fetch that data from list and assign to ComboBox.

Problem:

Data in list is objects consider example student class. There are many objects of student class in list. Now I want to assign
Combobox displaymember as Student Name
value memeber as Student Id.


I tried :

List studList = dm.getStudList();
 
foreach(Student s in studList)
{
ComboBox item = new ComboBox();
item.DisplayMember = s.getStudentName();
item.ValueMember = s.getStudentId().ToString();
cb_StudentName.Items.Add(item);
}


This code is not adding all names.
How we can do it ?

Please help me.

Thank you so much In Advance.

I will be grateful for you.

what i can gather from your question is that you have a generic list like
List <student> StudentList;</student>

You need to iterate through the list elements, which are Student objects and bind their properties (Id and Name, in your case) to the Value and Text fields of your control.
Use a foreach loop to iterate through the list.

Please improve the question if you mean something else entirely.


Quote:

foreach(Student s in studList)
{
ComboBox item = new ComboBox();
item.DisplayMember = s.getStudentName();
item.ValueMember = s.getStudentId().ToString();
cb_StudentName.Items.Add(item);
}


Look at your code block carefully. Here you are creating an object of combobox object, adding it's properties every time.
You should try something like this:

ComboBox item = new ComboBox();
item.DataSource = studList;
item.DisplayMember = "StudentName";
item.ValueMember = "StudentID";
//I'm not sure what is your cb_StudentName object doing here. Hope probably it is literal or some placeholder.
cb_StudentName.Items.Add(item);


Also I would suggest you to read these:
MSDN : ComboBox[^]
how to bind a list to a combobox? (Winforms)[^]

    --Amy


item is an instance of the ComboBox class. Hence it is a control. It is not a list item of a particluar ComboBox instance.

You don't need to create a ComboBox object , if you just want to add the list items. Same logic applies to any control.

You can try somethimg like this.

List studList = dm.getStudList();
 
foreach(Student s in studList)
{
ListItem item = new ListItem();
item.DisplayMember = s.getStudentName();
item.ValueMember = s.getStudentId().ToString();
cb_StudentName.Items.Add(item);
}