My problem is: when I bind datasource to DataGridView
BindingList<Contract> contracts = new BindingList<Contract>(Contract.GetAll());
dgEndingContracts.DataSource = contracts.Where(c => c.ExpirationDate <= nextMonth && c.IsArchived == false).ToList();
and set every column to SortMode = DataGridViewColumnSortMode.Automatic
when I click on dataGridView header rows doesn't sort.
But when I manually create each column, create and fill with data each row of dataGridView, and the set column sort mode to automatic, sorting works fine.
What is the difference and how can I enable sorting in first approach?
I 've found solution.
It's seems that DataGridView can't sort either List <T>
or BindingList<T>
So I've added class SortedBindingList<T>
based on code from:
and now my DataGridView
can sort columns.
Thanks for help guys.
.ToList() doesn't return something that implements IBindingList. Use something, like thtat:
dgEndingContracts.DataSource = new BindingList<Contract>(contracts.Where(c => c.ExpirationDate <= nextMonth && c.IsArchived == false).ToList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With