Wednesday, March 30, 2011

Filtering a list

In some cases I've been using a DataTable, filtering it witha DataView and displaying the DataView in a DataGrid. I've recently started switching to using my own classes. For example:

[Serializable]
[System.Xml.Serialization.XmlRoot("Items", Namespace = "http://mycomp.com/test")]
public class Items: List<Item>
{

}

[Serializable]
[System.Xml.Serialization.XmlRoot("Item", Namespace = "http://mycomp.com/test")]
public class Item
{
  //public properties here
}

I then take my Items class and display it in a DataGrid which has worked really well. Is there some way I can apply a filter to my list though? DataView only works on datatables.

From stackoverflow
  • If I understand your question correctly, you could use LINQ to Objects and bind the resulting list to the grid view...

  • You can use FindAll() method:

    List<Items> allItems = /* initialize list */;
    
    List<Items> filteredList =
        allItems.FindAll(item => item.Name.StartsWith("A"));
    

0 comments:

Post a Comment