随机播放 DevExpress GridControl 数据源

发布于 2024-09-17 10:10:27 字数 1119 浏览 2 评论 0原文

我需要打乱 GridControl 的数据源。我在 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;
        }
    }
}

然后我使用克隆、随机播放和附加到 SongsDataSource 属性的方法:

    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;
        }

奇怪的是,即使我使用 GridControl.RefreshDataSource(),它似乎也没有反映 GridControl 的更改设置 SongsDataSource 方法后的方法。如果我检查数据源顺序,则洗牌成功发生。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坠似风落 2024-09-24 10:10:27

由于您已经更改了最初设置为 DataSource 的对象,因此调用 RefreshDataSource() 不会有任何好处,因为您无法刷新不再存在的内容。您的问题在于:

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.

您可以按原样传递列表,而不需要克隆它。还要用 gridControl.BeginUpdate() end gridControl.EndUpdate() 包围 Shuffle() 方法调用,以防止在DataSource 的元素正在发生变化。

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.

饮惑 2024-09-24 10:10:27

我在使用 DevExpress GridControl 时遇到了这样的问题。我认为,这种情况是由 GridView 引起的(http://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraGridViewsGridGridViewtopic ),它会为每个 GridControl 自动创建。
这是 GridControl 的一部分,负责数据源的可视化。
如果您需要更改数据源,请尝试:

GridView.Columns.Clear();
GridControl.DataSource = You_New_DataSource;
GridView.RefreshData();
GridControl.RefreshDataSource(); 

I had such problems with DevExpress GridControl. I think, that this situation caused by GridView(http://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraGridViewsGridGridViewtopic), which creates automaticly for each GridControl.
This is part of GridControl responsible for visualization of DataSource.
If you need to change DataSource try:

GridView.Columns.Clear();
GridControl.DataSource = You_New_DataSource;
GridView.RefreshData();
GridControl.RefreshDataSource(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文