随机播放 DevExpress GridControl 数据源
我需要打乱 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您已经更改了最初设置为 DataSource 的对象,因此调用
RefreshDataSource()
不会有任何好处,因为您无法刷新不再存在的内容。您的问题在于:您可以按原样传递列表,而不需要克隆它。还要用
gridControl.BeginUpdate()
endgridControl.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:You can pass the list as it is, without the need of cloning it. Also surround the
Shuffle()
method call withgridControl.BeginUpdate()
endgridControl.EndUpdate()
to prevent any updates to the grid while the elements of theDataSource
are changing.我在使用 DevExpress GridControl 时遇到了这样的问题。我认为,这种情况是由 GridView 引起的(http://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraGridViewsGridGridViewtopic ),它会为每个 GridControl 自动创建。
这是 GridControl 的一部分,负责数据源的可视化。
如果您需要更改数据源,请尝试:
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: