Binding ComboBox to a BindingList Problem

G

Guest

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<MyStruct> myList = 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
 
M

Morten Wennevik [C# MVP]

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<MyStruct> myList = 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 );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top