0

Problem: I have a C# DataGridView (Windows Forms). I've set its DataSource to a custom SortableBindingList I've found online. When I add/remove items to/from the bound list, the DataGridView doesn't update until I reset the DataSource. HOWEVER, if I don't use this custom SortableBindingList and use the standard BindingList, then it works just as expected. But I need to be able to sort. I am using EntityFramework, if that's of any help.

I've tried: Making my entities inherit INotifyPropertyChanged. This doesn't help for whatever reason. I've also tried various SortableBindingLists from the web and haven't found one to work as I need it to in my context. I've tried modifying this one I've found and have had no luck thus far. It's hard to be specific without making this post 10,000 lines long.

Asking For: A working SortableBindingList that can be sorted programatically and has binding (adding/removing from the list is reflected on the DataGridView). It would, ideally, used similarly to how I'm currently using it in the code below (to prevent too much refactoring). Or some fix to what I'm doing wrong as it's probably really obvious. The SortableBindingList I'm using was taken from this article.

Some code that may help:

Using the below binds correctly and adds/removes as expected, but I cannot sort:

this.binding.DataSource = this.context.SomeEntityList.Local.ToBindingList();

where "SomeEntityList" is of type IDbSet<SomeEntity>

--

Using any of the below code allows me to sort, but doesn't add/remove as expected:

this.binding.DataSouce = new SortableBindingList<SomeEntity>(this.context.SomeEntityList);
this.binding.DataSouce = new SortableBindingList<SomeEntity>(this.context.SomeEntityList.ToList());
this.binding.DataSouce = new SortableBindingList<SomeEntity>(this.context.SomeEntityList.Local);
this.binding.DataSouce = new SortableBindingList<SomeEntity>(this.context.SomeEntityList.Local.ToList());
this.binding.DataSouce = new SortableBindingList<SomeEntity>(this.context.SomeEntityList.Local.ToBindingList());

--

I'm setting the DataGridView's DataSouce like so:

this.DataGridView.DataSouce = this.binding;

--

Any help is much appreciated!

Thanks, Andy

4

1 回答 1

2

我感觉到你的痛苦,我在同一个问题上挣扎了好几个小时。事实证明,您无法对自定义对象列表进行排序,无论它是 SortableBindingList 还是 BindingList。

尝试改用 BindingListView: http: //blw.sourceforge.net/ 使用 BindingListView 作为 DataGridView 的来源。这将允许您拥有一个可排序的列表,并且在添加或删除项目后更新它没有问题。

如果您需要示例解决方案,请告诉我。

于 2013-06-14T00:53:33.247 回答