Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 654
Patrice Cote Avatar asked Jul 09 '26 03:07

Patrice Cote


1 Answers

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!

like image 181
Homam Avatar answered Jul 11 '26 10:07

Homam