• Blog
  • About
  • Contact
linkedin
twitter

Blog Post

24
APR
2006

BindingListView – The DataView equivalent for custom collections

by : Christoph De Baene
comment : 0

BindingListView<T> is a project hosted on GotDotNet that gives you search, sorting and filtering capabilities to a plain BindingList. For example if you have the following Person entity:

[csharp]
public class Person
{
string firstName = string.Empty;
int age = 0;

public string FirstName
{
get { return firstName; }
set { firstName = value; }
}

public int Age
{
get { return age; }
set { age = value; }
}

public Person(string firstName, int age)
{
this.firstName = firstName;
this.age = age;
}
}
[/csharp]

And you want to have a collection of persons bind to a datagrid, you can simply write the following:

[csharp]
BindingList<Person> persons = new BindingList<Person>();
persons.Add(new Person("Bill", 45));
persons.Add(new Person("Gert", 33));
persons.Add(new Person("Johan", 12));
persons.Add(new Person("An", 27));

personsGrid.DataSource = persons;
[/csharp]

What if you need to do some filtering, or simply sorting on the datagrid? Therefore you would need to create a custom collection class that implements a bunch of interfaces for having sorting, filtering and searching capabilities.

The BindingListView<T> class has all these functionalities built-in. You simply have to pass a list, and bind the BindingListView to the datagrid. The same way you would do with a DataSet and DataView. This means:

[csharp]
BindingListView<Person> personsView = new BindingListView<Person>(persons);
personsGrid.DataSource = personsView;
[/csharp]

Sorting on a BindingListView is done through the Sort property, the same as on a DataView. For example:

[csharp]
personsView.Sort = "FirstName";
[/csharp]

Filtering is done through the Filter property and uses anonymous delegates. So for example to filter all persons that are less than 30, you can write:

[csharp]
personsView.Filter = BindingListView<Person>.CreateItemFilter(new Predicate<Person>(
delegate(Person person)
{
return (person.Age < 30);
}
));
[/csharp]

You can also merge multiple sources to one view (functionality that the DataView doesn’t support) through the AggregatedBindingListView<T>.

About the Author

Social Share

    Leave a Reply Cancel reply

    You must be logged in to post a comment.

    Tag cloud

    .NET SDK Ajax Architecture ASP.NET ASP.NET Web API Code Generation Components Events Forms Hardware Modeling Neo4j Patterns & Practices Personal Roslyn Services Silverlight SQL Server Testing Tools Unity Utils & Tools Virtualization Visual studio Windows Live WPF

    Archive

    • April 2012
    • October 2011
    • August 2011
    • April 2011
    • March 2011
    • July 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • August 2008
    • March 2008
    • December 2007
    • November 2007
    • October 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • July 2005
    • June 2005
    • May 2005
    • April 2005
    • March 2005
    • February 2005
    • September 2004
    • July 2004
    • June 2004
    • May 2004
    • April 2004
    • March 2004
      Copyright 2013, Christoph De Baene