Filtering WinForm DataGridView
I have a DataGridView control that is bound to a custom typed list (that inherits BindingList). I would开发者_运维百科 like to be able to filter rows based on a simple column value (bool type). Ultimately, the fonctional goal is to be able to mark an item as deleted but just flag it as deleted in the DataSource, not remove it. Juste remove it from the grid, not the DataSource.
Any idea ?
You can use LINQ to filter your data then create a new BindingList and reassign it to the dataGridView.
Assuming you have a flag in the person class called WillBeDeleted
:
dataGridView1.DataSource = new SortableBindingList<Person>
(SampleData.Where(p => !p.WillBeDeleted).ToList());
Good luck!
Just to make it clearer, I used this code to create the SortableBindingList http://www.timvw.be/presenting-the-sortablebindinglistt-take-two/ (I translated it to VB.NET)
Then, I have my own collection object that contains properties and a SortableBindingList of my entities.
Private mListeNotes As New SP1ZSortableBindingList(Of SP5004ZNoteEvolutiveEntite)
And that is what I bind my grid to so I it is now sortable. So I need it to remain of that type, not generic list.
精彩评论