473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Binding ComboBox to a BindingList Problem

Hello,

I have a ComboBox named comboBoxSelChannel.

I declared a structure named MySturct.

public struct MyStruct
{
public int Index;
public string Name;
}

I created a BindingList of MyStruct:

MyStruct myStruct = new MyStruct() ;
BindingList<MyStructmyList = new BindingList<MyStruct>();

I added 4 MyStruct elements to my BindingList:

myStruct.Name = "Name1" ;
myStruct.Index = 0;
myList.Add( myStruct );
myStruct.Name = "Name2";
myStruct.Index = 1;
myList.Add( myStruct );
myStruct.Name = "Name3";
myStruct.Index = 2;
myList.Add( myStruct );
myStruct.Name = "Name4";
myStruct.Index = 3;
myList.Add( myStruct );

I Binded the ComboBox to the BindingList:

comboBoxSelChannel.DataSource = myList;
comboBoxSelChannel.DisplayMember = "Name";
comboBoxSelChannel.ValueMember = "Index";

The ComboBox is showing 4 elements but instead of showing Name1, Name2,
Name3, Name4 it shows 4 times "MyStruct".

Any Idea why and what I am doing wrong here?

Thanks
Eitan
Jun 29 '07 #1
1 5089
On Fri, 29 Jun 2007 17:06:02 +0200, Eitan <Ei***@discussions.microsoft.comwrote:
Hello,

I have a ComboBox named comboBoxSelChannel.

I declared a structure named MySturct.

public struct MyStruct
{
public int Index;
public string Name;
}

I created a BindingList of MyStruct:

MyStruct myStruct = new MyStruct() ;
BindingList<MyStructmyList = new BindingList<MyStruct>();

I added 4 MyStruct elements to my BindingList:

myStruct.Name = "Name1" ;
myStruct.Index = 0;
myList.Add( myStruct );
myStruct.Name = "Name2";
myStruct.Index = 1;
myList.Add( myStruct );
myStruct.Name = "Name3";
myStruct.Index = 2;
myList.Add( myStruct );
myStruct.Name = "Name4";
myStruct.Index = 3;
myList.Add( myStruct );

I Binded the ComboBox to the BindingList:

comboBoxSelChannel.DataSource = myList;
comboBoxSelChannel.DisplayMember = "Name";
comboBoxSelChannel.ValueMember = "Index";

The ComboBox is showing 4 elements but instead of showing Name1, Name2,
Name3, Name4 it shows 4 times "MyStruct".

Any Idea why and what I am doing wrong here?

Thanks
Eitan
Hi Eitan,

You are updating the same struct before adding it again to the list. Try

myStruct = new MyStruct();
myStruct.Name = "Name1" ;
myStruct.Index = 0;
myList.Add( myStruct );
myStruct = new MyStruct();
myStruct.Name = "Name2";
myStruct.Index = 1;
myList.Add( myStruct );
myStruct = new MyStruct();
myStruct.Name = "Name3";
myStruct.Index = 2;
myList.Add( myStruct );
myStruct = new MyStruct();
myStruct.Name = "Name4";
myStruct.Index = 3;
myList.Add( myStruct );

--
Happy coding!
Morten Wennevik [C# MVP]
Jun 29 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
0
by: g18c | last post by:
Hi, i have a class which has a property which is a list of items. I want to be able to set this item list from a set of items (in this case list2 as shown in the code below) from a drop down combo...
4
by: kin | last post by:
After a I read Walkthrough: Connecting to Data in Objects (http://msdn2.microsoft.com/en-us/library/ms171892.aspx#Mtps_DropDownFilterText). I still have project on that. My School project...
19
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks...
3
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
4
by: jehugaleahsa | last post by:
Hello: We are working on a large number of forms. The original approach was to use the built-in data designer, type-specific DataTables and form binding. However, we are running into various...
11
by: dave18 | last post by:
Hello all! I found a solution to my original question, but there's still so much I don't understand about it, I thought I'd give this forum a try. At the very least, maybe it will help someone...
3
by: Marc Gravell | last post by:
Sure; just set the DataSource, DisplayMember and ValueMember of a DataGridViewComboBoxColumn (below ). Marc using System.Collections.Generic; using System.Windows.Forms; using...
4
by: csharpula csharp | last post by:
Hello, I would like to know how can I copy from BindingList<objto some other BindingList<obj>? I tried to pass one binding list to other via constractor but this is copying it by reference and I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.