One datasource to multiple controls

Posted on 2009-04-29
Medium Priority
Last Modified: 2012-05-06
Hi people,
var albums = new AlbumCollection();
            ISortExpression SortOrder = new SortExpression(AlbumFields.Order | SortOperator.Ascending);
            //  read rows from database
            albums.GetMulti(null, int.MaxValue, SortOrder);
            // bind it to listbox1
            LatestAlbum1List.DataSource = albums;
            LatestAlbum1List.DisplayMember = "Name";
            // bind it to listbox2
            LatestAlbum2List.DataSource = albums;
            LatestAlbum2List.DisplayMember = "Name";
            // set SelectedIndex
            LatestAlbum1List.SelectedIndex = 12;
            // when I do this, even LatestAlbum2List 
            // also changes the SelectedIndex to 12
            // why does this happen and how do I break this association?

Open in new window

Question by:nmarun
1 Comment
LVL 27

Accepted Solution

nmarun earned 0 total points
ID: 24262766
I resolved this issue by using BindingSource object.

var albums = new AlbumCollection();
ISortExpression SortOrder = new SortExpression(AlbumFields.Order | SortOperator.Ascending);
albums.GetMulti(null, int.MaxValue, SortOrder);
// create two BindingSource instances
var source1 = new BindingSource();
var source2 = new BindingSource();
LatestAlbum1List.ValueMember = "AlbumId";
LatestAlbum1List.DisplayMember = "Name";
// assign source1's datasource
source1.DataSource = albums;
// set source1 as listbox's datasource
LatestAlbum1List.DataSource = source1;
LatestAlbum2List.ValueMember = "AlbumId";
LatestAlbum2List.DisplayMember = "Name";
// assign source2's datasource
source2.DataSource = albums;
// set source2 as listbox's datasource
LatestAlbum2List.DataSource = source2;

Open in new window


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question