One datasource to multiple controls

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

LVL 27
nmarunAsked:
Who is Participating?
 
nmarunConnect With a Mentor Author Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.