0
votes

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

1
If your intent is only to sort the list, you could use an ObservableCollection<MyType> and define an ICollectionView on that, which allows you to sort the collection based on a property name in MyType. - PoweredByOrange
It needs to be able to be sorted programatically and bound so that when you add/remove/edit, the DataGridView reflects this. Currently I can only get one or the other (though edits work... just not add/remove) - aladd04
ICollectionView should do this for you. You'll find many examples on Google. - PoweredByOrange
Google searches are leading me to that being useful for WPF and the DataGrid tool. I'm needing help for Windows Forms and the DataGridView tool. If you can provide an example for my situation, that would be great! - aladd04
Oops! I totally thought you were talking about WPF... - PoweredByOrange

1 Answers

1
votes

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.