BindingListOfT

  • Thread starter Thread starter David Veeneman
  • Start date Start date
D

David Veeneman

I'm using the new BindingList generic class in a collection class. For those
not familiar with BindingLists, they are similar to generic ArrayLists, but
they implement the IBindingList interface, which means they support two-way
data binding.

As my class is written, the collection's BindingList is exposed as a
property. That means I have to bind to the property, rather than the
collection, like this:

dataGridView1.DataSource = myCollection.BindingList;

I'd rather bind directly to the collection, rather than to a property, like
this:

dataGridView1.DataSource = myCollection;

Is there a way to accomplish this with a BindingList? Or, is there a base
class that implements IBindingList that I could derive from? Thanks in
advance for your help.

David Veeneman
Foresight Systems
 
Hi,

David Veeneman said:
I'm using the new BindingList generic class in a collection class. For
those not familiar with BindingLists, they are similar to generic
ArrayLists, but they implement the IBindingList interface, which means
they support two-way data binding.

As my class is written, the collection's BindingList is exposed as a
property. That means I have to bind to the property, rather than the
collection, like this:

dataGridView1.DataSource = myCollection.BindingList;

I'd rather bind directly to the collection, rather than to a property,
like this:

dataGridView1.DataSource = myCollection;

Is there a way to accomplish this with a BindingList? Or, is there a base
class that implements IBindingList that I could derive from?

Maybe i'm missing something but you can derive from BindingList<T>, eg:

public class PersonCollection : BindingList<Person>
{
}

HTH,
Greetings



Thanks in
 
Hi David,

Assuming you have implemented a custom collection to keep track of your data.
You can replace the collection with a declaration like this:

BindingList<Person> persons = new BindingList<Person>();

And bind it to the datagridview

DataGridView1.DataSource = persons;
 
David Veeneman said:
So simple, I completely overlooked it! Thanks.

Yep.

Inheritance is somewhat hard to grasp for some. Generics often more so. We
all master them some day and know the idioms and their use, and when not to.
But combined they make for some serious braindamage when thinking on how to
solve stuff, while the solution often turn out to be quite simple..

I was lost at this one and was about to submit a 'it can't be done' post
when I saw the solution...

All too easy..

Happy Coding
- Michael S
 
Back
Top