Patrice Cote
Patrice Cote

Reputation: 3716

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 ?

Upvotes: 3

Views: 2280

Answers (2)

Homam
Homam

Reputation: 23871

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!

Upvotes: 4

Patrice Cote
Patrice Cote

Reputation: 3716

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.

Upvotes: 0

Related Questions