Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C#
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.
Posted
Updated 26-Jul-14 1:16am
v3
Comments
OriginalGriff 26-Jul-14 6:58am    
What have you tried?
Where are you stuck?
What help do you need?
Use the "Improve question" widget to edit your question and provide better information.
[no name] 26-Jul-14 7:07am    
Dear sir,
I tried

List<state> 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.
_Amy 26-Jul-14 7:09am    
What do you mean by C#? We don't have access to your system. Click on the Green Button above and improve your question.

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.
 
Share this answer
 
Comments
[no name] 26-Jul-14 7:17am    
Thank you Abhijit for your suggestion.

I improved question.
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
 
Share this answer
 
Comments
[no name] 26-Jul-14 7:25am    
I read that.

I tried code.

Only two names get added in combobox.
cb_StudentName is combobox id.
_Amy 26-Jul-14 7:29am    
Then try this:
cb_StudentName.DataSource = studList;
cb_StudentName.DisplayMember = "StudentName";
cb_StudentName.ValueMember = "StudentID";
[no name] 26-Jul-14 7:36am    
Exception Occured

System.ArgumentException was unhandled
Message=Cannot bind to the new display member.
Parameter name: newDisplayMember
Source=System.Windows.Forms
ParamName=newDisplayMember
StackTrace:
at System.Windows.Forms.ListControl.SetDataConnection(Object newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
at System.Windows.Forms.ListControl.set_ValueMember(String value)
at Anand_Automobiles.DocumentEntryWindow..ctor(Int32 Request) in E:\Anand Automobiles\1.Development\Anand_Automobiles\Forms\Document Entry Forms\DocumentEntryWindow.cs:line 44
at Anand_Automobiles.Anand_Automobiles.serviceEntryToolStripMenuItem_Click(Object sender, EventArgs e) in E:\Anand Automobiles\1.Development\Anand_Automobiles\Forms\Anand_Automobiles.cs:line 130
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Anand_Automobiles.Program.Main() in E:\Anand Automobiles\1.Development\Anand_Automobiles\Global Data\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
_Amy 26-Jul-14 7:39am    
Please make sure that StudentID and StudentName properties are Public.
Like:
public string StudentID
{
get { return _value ; }
set { _value = value; }
}
public string StudentName
{
get { return _value ; }
set { _value = value; }
}
[no name] 26-Jul-14 7:51am    
Ya ofcourse
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.

C#
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);
}
 
Share this answer
 
Comments
[no name] 26-Jul-14 7:49am    
What is ListItem ?
Abhijit Ghosh (Subho) 26-Jul-14 7:56am    
You can read about the ListItem class anywhere. Try msdn.
[no name] 26-Jul-14 8:02am    
Dear Abhijeet,
I search about ListItem is a part of System.Windows.Documents also System.Web.UI.WebControls .
I am not able to find how to use them in Windows Application.
If you know please
Abhijit Ghosh (Subho) 26-Jul-14 8:30am    
I wasn't trying to post the exact solution. You can't use ListItem in your case. I was trying to explain why you can't create an instance of ComboBox.

In your case, just these three lines should do it

cb_StudentName.DataSource = StudList;
cb_StudentName.DisplayMember = "StudentName";
cb_StudentName.ValueMember = "StudentID";

But the code has already been suggested previously. It should work fine. If not, what errors are you facing?
[no name] 26-Jul-14 8:39am    
All names are not added.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900