当绑定BindingList被更改时,DataGridView不会更新 [英] DataGridView not updating when bound BindingList is changed

查看:311
本文介绍了当绑定BindingList被更改时,DataGridView不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我有一个C# DataGridView (Windows窗体)。我已经将它的DataSource设置为一个自定义的 SortableBindingList 我已经在网上找到了。当我从绑定列表中添加/删除项目时,DataGridView不会更新,直到我重置DataSource。然而,如果我不使用这个自定义的SortableBindingList并使用标准的BindingList,那么它的工作原理就如预期的那样。但我需要排序。我正在使用EntityFramework,如果这有任何帮助。



我试过:让我的实体继承 INotifyPropertyChanged 。这并不因为什么原因而有所帮助我也尝试过各种可从网络上的SortableBindingLists,并没有找到一个工作,因为我需要它在我的上下文。我已经尝试修改我发现的那个,迄今没有运气。请问:一个可以按程序排序并具有绑定功能的SortableBindingList(SortableBindingList),这是一个非常有用的功能,从列表中添加/删除反映在DataGridView上)。理想情况下,我将在下面的代码中类似地使用它(以防止太多的重构)。或者一些修复我做错了什么,因为它可能是很明显的。我使用的SortableBindingList是从这篇文章



可能有帮助的一些代码:



使用以下绑定正确,按预期添加/删除,但我无法排序:

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

其中SomeEntityList的类型为 IDbSet< SomeEntity>



-



使用下面的代码可以让我排序,但不会按预期添加/删除:

  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());

-



我正在设置DataGridView的DataSouce如下所示:

  this.DataGridView.DataSouce = this.binding; 

-



任何帮助都是谢谢,谢谢,Andy

解决方案

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



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



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

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

解决方案

I feel your pain, I struggled with the same problem for many hours. It turns out that you cannot sort a list of custom objects, regardless if it is SortableBindingList or BindingList.

Try using the BindingListView instead: http://blw.sourceforge.net/ Use the BindingListView as the soruce for your DataGridView. This will allow you to have a sortable list and it has no problems with updating after you add or remove items.

Let me know if you need an example solution.

这篇关于当绑定BindingList被更改时,DataGridView不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆