Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Binding ComboBox to a BindingList Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eitan - 29 Jun 2007 16:06 GMT
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
Morten Wennevik [C# MVP] - 29 Jun 2007 16:55 GMT
> Hello,
>
[quoted text clipped - 41 lines]
> 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 );

Signature

Happy coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.