views:

137

answers:

1

Hello,

I need to shuffle the GridControl's DataSource. I use this property in a UserControl:

private List<Song> _songsDataSource;
public List<Song> SongsDataSource
{
    get { return _songsDataSource; }
    set
    {
        _songsDataSource = value;
        if (!value.IsNull())
        {
            SongsBindingList = new BindingList<Song>(value);
            songsBinding.DataSource = SongsBindingList;
        }
    }
}

Then i use a method that i clone, shuffle and append to the SongsDataSource property:

    List<Song> newList = HelpClasses.Shuffle((List<Song>) SongsDataSource.Clone());
    SongsDataSource = newList;

public static List<Song> Shuffle(List<Song> source)
        {
            for (int i = source.Count - 1; i > 0; i--)
            {
                int n = rng.Next(i + 1);
                Song tmp = source[n];
                source[n] = source[i - 1];
                source[i - 1] = tmp;
            }
            return source;
        }

Strange thing is that it doesn't seem to reflect the changes to the GridControl even i use the GridControl.RefreshDataSource() method after set the SongsDataSource method. If i check the DataSource order, shuffle was happened successfully.

Thank you.

A: 

Since you've changed the object originally set as a DataSource, calling RefreshDataSource() won't do any good cause you can't refresh something that's no longer there. Your problem is here:

List<Song> newList = HelpClasses.Shuffle((List<Song>) SongsDataSource.Clone());
SongsDataSource = newList;   // the reference has changed, the grid doesn't know what to do when RefreshDataSource() is called.

You can pass the list as it is, without the need of cloning it. Also surround the Shuffle() method call with gridControl.BeginUpdate() end gridControl.EndUpdate() to prevent any updates to the grid while the elements of the DataSource are changing.

devnull
I'm passing on the Shuffle() method the BindingSource.DataSource and still nothing happens, where is a BindingList<Song>. Added the BeginUpdate(), EndUpdate() methods and after called the RefreshDatasource() with no luck still.
try setting the List<Song> as DataSource without using a BindingList<T>. eg. gridControl.DataSource = SongsDataSource.
devnull
also check if you have sorting on any of the columns, cause that would automatically sort the records in your grid and shuffling would be useless.
devnull