One datasource to multiple controls

Posted on 2009-04-29
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

    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Delegate in C# 3 38
    Refactor .net copy files method 8 39
    Change to event 1 52
    Build JSON from table records 17 6
    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…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now