Converting List<T> structures to BindingList<T>

G

Guest

Hi,

I am having trouble realising how to data bind a hierarchical structure for
data binding to a hierarchical grid.

My aim is to have a hierarchical structure built of List<T> collections and
convert it to BindingList<T>. The reason for this is that the former could
then be used as a lighter structure in a component where no user interface is
required.

I have partially achieved this as follows in this concocted example:

I have created a generic class inheriting from BindingList<T> and
implementing IBindingListView defined as follows:

public class BindingListView<T> : BindingList<T>, IBindingListView

I inherited from this and provided a constructor to load a collection
List<Person>

public class PersonCollectionBindingListView : BindingListView<Person>
{
public PersonCollectionBindingListView() : base() { }

public PersonCollectionBindingListView(List<Person> objPersonCollection)
: base(objPersonCollection)
{
}

protected override object AddNewCore()
{
Person objPerson = new Person();
objPerson.PersonID = 0;
objPerson.PersonName = "[Enter Person Name]";
this.Add(objPerson);
return objPerson;
}

//Other code removed for brevity
}

The Person class implements IDataErrorInfo, IEditableObject,
INotifyPropertyChanged (not shown):

public class Person:System.Object, IDataErrorInfo, IEditableObject,
INotifyPropertyChanged
{
private System.Int32 PersonID;
private System.String PersonName;
private TelephoneNumbersCollection TelephoneNumbersCollection;

//code removed for brevity

}

But notice that Person contains a collection for telephone numbers which
based on <List>TelephoneNumber (not shown) i.e.
public class TelephoneNumbersCollection:List<TelephoneNumber>

For the first level, everything works fine. I can create a collection of
type <List>Person and pass it to the PersonCollectionBindingListView
constructor. When I bind to the grid this works fine.

But I am stuck with what to do about the collection of telephone numbers. In
order to interact with the grid propertly I would need to convert the
collection to inherit from: public class BindingListView<T> : BindingList<T>,
IBindingListView

i.e.
public class TelephoneCollectionBindingListView :
BindingListView<TelephoneNumber>

Where TelephoneNumber would need to implement IDataErrorInfo,
IEditableObject, InotifyPropertyChanged as the Person class does.

But since I am passing in a collection of <List>Person, it’s telepnone
collection is based on List not BindingList.

Since I wanted to have a simple structure without all the binding interfaces
so that I can use the application as a simple component. I am wondering how
to best convert from one structure to the next.

I hope I am not asking the obvious but can anyone please help me?

Also I have tried to keep this as simple as possible, by cutting out a lot
of code, please let me know if more information is required.

Best Regards,

Steve.
 
G

Guest

Hi,
I am not expert in this but when i searched on internet i found in below
msdn link converts List<T> into BindingList<T> with just one line of code:
BindingList<Order> orderList = new BindingList<Order>(Order.GetEntityList());
Click on Figure 6 in below link and you will get further insights:
http://msdn.microsoft.com/msdnmag/issues/07/02/DataPoints/default.aspx
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



Steve Bugden said:
Hi,

I am having trouble realising how to data bind a hierarchical structure for
data binding to a hierarchical grid.

My aim is to have a hierarchical structure built of List<T> collections and
convert it to BindingList<T>. The reason for this is that the former could
then be used as a lighter structure in a component where no user interface is
required.

I have partially achieved this as follows in this concocted example:

I have created a generic class inheriting from BindingList<T> and
implementing IBindingListView defined as follows:

public class BindingListView<T> : BindingList<T>, IBindingListView

I inherited from this and provided a constructor to load a collection
List<Person>

public class PersonCollectionBindingListView : BindingListView<Person>
{
public PersonCollectionBindingListView() : base() { }

public PersonCollectionBindingListView(List<Person> objPersonCollection)
: base(objPersonCollection)
{
}

protected override object AddNewCore()
{
Person objPerson = new Person();
objPerson.PersonID = 0;
objPerson.PersonName = "[Enter Person Name]";
this.Add(objPerson);
return objPerson;
}

//Other code removed for brevity
}

The Person class implements IDataErrorInfo, IEditableObject,
INotifyPropertyChanged (not shown):

public class Person:System.Object, IDataErrorInfo, IEditableObject,
INotifyPropertyChanged
{
private System.Int32 PersonID;
private System.String PersonName;
private TelephoneNumbersCollection TelephoneNumbersCollection;

//code removed for brevity

}

But notice that Person contains a collection for telephone numbers which
based on <List>TelephoneNumber (not shown) i.e.
public class TelephoneNumbersCollection:List<TelephoneNumber>

For the first level, everything works fine. I can create a collection of
type <List>Person and pass it to the PersonCollectionBindingListView
constructor. When I bind to the grid this works fine.

But I am stuck with what to do about the collection of telephone numbers. In
order to interact with the grid propertly I would need to convert the
collection to inherit from: public class BindingListView<T> : BindingList<T>,
IBindingListView

i.e.
public class TelephoneCollectionBindingListView :
BindingListView<TelephoneNumber>

Where TelephoneNumber would need to implement IDataErrorInfo,
IEditableObject, InotifyPropertyChanged as the Person class does.

But since I am passing in a collection of <List>Person, it’s telepnone
collection is based on List not BindingList.

Since I wanted to have a simple structure without all the binding interfaces
so that I can use the application as a simple component. I am wondering how
to best convert from one structure to the next.

I hope I am not asking the obvious but can anyone please help me?

Also I have tried to keep this as simple as possible, by cutting out a lot
of code, please let me know if more information is required.

Best Regards,

Steve.
 
G

Guest

Hi,
I tried myself and found that indeed one can convert List<T> into
BindingList<T> structure with just one line of code.Below code is working
perfectly well in my machine:
[1]
List<Customer> lc = new List<Customer>();

lc.Add(new Customer(0, "Mr. Zero", 10.0M));
lc.Add(new Customer(1, "Mrs. One", 15.0M));
lc.Add(new Customer(2, "Dr. Two", 20.0M));


/*******************************************************************
* Bind the DataGridView to the list of Customers using Complex
* binding (customer business type is shown above).

******************************************************************/
BindingList < Customer > blc = new BindingList<Customer>(lc) ;
this.dataGridView1.DataSource = blc;
[2] Code for Customer.cs Class:
public class Customer
{
/* Private variables */
private int _id;
private string _name;
private Decimal _rate;

/* Constructor */
public Customer()
{
this.ID = -1;
this.Name = string.Empty;
this.Rate = 0.0M;
}

public Customer(int id, string name, Decimal rate)
{
this.ID = id;
this.Name = name;
this.Rate = rate;
}

/* Public API */
public int ID
{
get { return _id; }
set { _id = value; }
}

public string Name
{
get { return _name; }
set { _name = value; }
}

public Decimal Rate
{
get { return _rate; }
set { _rate = value; }
}
}
Please do let me know if you still have any doubts.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



Steve Bugden said:
Hi,

I am having trouble realising how to data bind a hierarchical structure for
data binding to a hierarchical grid.

My aim is to have a hierarchical structure built of List<T> collections and
convert it to BindingList<T>. The reason for this is that the former could
then be used as a lighter structure in a component where no user interface is
required.

I have partially achieved this as follows in this concocted example:

I have created a generic class inheriting from BindingList<T> and
implementing IBindingListView defined as follows:

public class BindingListView<T> : BindingList<T>, IBindingListView

I inherited from this and provided a constructor to load a collection
List<Person>

public class PersonCollectionBindingListView : BindingListView<Person>
{
public PersonCollectionBindingListView() : base() { }

public PersonCollectionBindingListView(List<Person> objPersonCollection)
: base(objPersonCollection)
{
}

protected override object AddNewCore()
{
Person objPerson = new Person();
objPerson.PersonID = 0;
objPerson.PersonName = "[Enter Person Name]";
this.Add(objPerson);
return objPerson;
}

//Other code removed for brevity
}

The Person class implements IDataErrorInfo, IEditableObject,
INotifyPropertyChanged (not shown):

public class Person:System.Object, IDataErrorInfo, IEditableObject,
INotifyPropertyChanged
{
private System.Int32 PersonID;
private System.String PersonName;
private TelephoneNumbersCollection TelephoneNumbersCollection;

//code removed for brevity

}

But notice that Person contains a collection for telephone numbers which
based on <List>TelephoneNumber (not shown) i.e.
public class TelephoneNumbersCollection:List<TelephoneNumber>

For the first level, everything works fine. I can create a collection of
type <List>Person and pass it to the PersonCollectionBindingListView
constructor. When I bind to the grid this works fine.

But I am stuck with what to do about the collection of telephone numbers. In
order to interact with the grid propertly I would need to convert the
collection to inherit from: public class BindingListView<T> : BindingList<T>,
IBindingListView

i.e.
public class TelephoneCollectionBindingListView :
BindingListView<TelephoneNumber>

Where TelephoneNumber would need to implement IDataErrorInfo,
IEditableObject, InotifyPropertyChanged as the Person class does.

But since I am passing in a collection of <List>Person, it’s telepnone
collection is based on List not BindingList.

Since I wanted to have a simple structure without all the binding interfaces
so that I can use the application as a simple component. I am wondering how
to best convert from one structure to the next.

I hope I am not asking the obvious but can anyone please help me?

Also I have tried to keep this as simple as possible, by cutting out a lot
of code, please let me know if more information is required.

Best Regards,

Steve.
 
G

Guest

Hi Manish,

Thankyou for your answers. esp. for the code example. I am sorry for the
delay, I was out most of yesterday.

As you suggest a BindingList can be created using the constructor and
passing in a collection of List<Person>. That works perfectly for the top
level and this is what my code in the original question shows.

But as I want to create a multi-level structure of BindingList objects there
is a problem.
I have used List<TelephoneNumber> to create a hierarchy, but of course there
could be many levels in a real application.

In order to get the whole structure to bind for updates, I need to make the
Telephone collection of type BindingList as follows:

public class PersonBLObject: Person
{
private TelephoneCollectionBindingList
mTelephoneNumbersColBindinglist;

public new TelephoneCollectionBindingList TelephoneNumbersCol
{
get {
return mTelephoneNumbersColBindinglist; }
set { mTelephoneNumbersColBindinglist = value; }
}

}


This simply inherits from Person, replacing the Telephone Collection with
one of type BindingList as shown below:


public class TelephoneCollectionBindingList :
BindingListView<TelephoneNumbers>
{
public TelephoneCollectionBindingList() : base() { }

public TelephoneCollectionBindingList(List<TelephoneNumbers>
objTelehoneNumbersCollection)
: base(objTelehoneNumbersCollection) {}

//Code removed for brevity
}


But now the person BindingList class has to change to take the new Person
class 'PersonBLObject':

public class PersonBLObjectCollectionBindingListView :
BindingListView<PersonBLObject>
{

public PersonBLObjectCollectionBindingListView() : base() { }

public PersonBLObjectCollectionBindingListView(List<PersonBLObject>
objPersonBLObjectCollection)
: base(objPersonBLObjectCollection){}

//Code removed for brevity

}


I already have the structure built using List<T>, it is able to query the
database and populate itself. This gives the lighter weight structure for use
in a component where no user-interface is required.

To convert the hierarchical structure from one built using List<T> to one of
BindingList<T>
I could write some foreach statements to load the BIndingList structure. I
will need to write a converter for each object, just for Person to
PersonBLObject in this case.

Obviously this is more work than simply calling the constructor, and it
would require reworking everytime the structure changes. Also, there will be
a performance hit for converting every Person object individually.

Is there a better way to go from one structure to the other?

Best Regards,

Steve.

PS. Having an inherited class 'PersonBLObject' is quite handy as it can
implement the interfaces: IDataErrorInfo, IEditableObject and
INotifyPropertyChanged. This keeps the Person object.


Manish Bafna said:
Hi,
I tried myself and found that indeed one can convert List<T> into
BindingList<T> structure with just one line of code.Below code is working
perfectly well in my machine:
[1]
List<Customer> lc = new List<Customer>();

lc.Add(new Customer(0, "Mr. Zero", 10.0M));
lc.Add(new Customer(1, "Mrs. One", 15.0M));
lc.Add(new Customer(2, "Dr. Two", 20.0M));


/*******************************************************************
* Bind the DataGridView to the list of Customers using Complex
* binding (customer business type is shown above).

******************************************************************/
BindingList < Customer > blc = new BindingList<Customer>(lc) ;
this.dataGridView1.DataSource = blc;
[2] Code for Customer.cs Class:
public class Customer
{
/* Private variables */
private int _id;
private string _name;
private Decimal _rate;

/* Constructor */
public Customer()
{
this.ID = -1;
this.Name = string.Empty;
this.Rate = 0.0M;
}

public Customer(int id, string name, Decimal rate)
{
this.ID = id;
this.Name = name;
this.Rate = rate;
}

/* Public API */
public int ID
{
get { return _id; }
set { _id = value; }
}

public string Name
{
get { return _name; }
set { _name = value; }
}

public Decimal Rate
{
get { return _rate; }
set { _rate = value; }
}
}
Please do let me know if you still have any doubts.
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



Steve Bugden said:
Hi,

I am having trouble realising how to data bind a hierarchical structure for
data binding to a hierarchical grid.

My aim is to have a hierarchical structure built of List<T> collections and
convert it to BindingList<T>. The reason for this is that the former could
then be used as a lighter structure in a component where no user interface is
required.

I have partially achieved this as follows in this concocted example:

I have created a generic class inheriting from BindingList<T> and
implementing IBindingListView defined as follows:

public class BindingListView<T> : BindingList<T>, IBindingListView

I inherited from this and provided a constructor to load a collection
List<Person>

public class PersonCollectionBindingListView : BindingListView<Person>
{
public PersonCollectionBindingListView() : base() { }

public PersonCollectionBindingListView(List<Person> objPersonCollection)
: base(objPersonCollection)
{
}

protected override object AddNewCore()
{
Person objPerson = new Person();
objPerson.PersonID = 0;
objPerson.PersonName = "[Enter Person Name]";
this.Add(objPerson);
return objPerson;
}

//Other code removed for brevity
}

The Person class implements IDataErrorInfo, IEditableObject,
INotifyPropertyChanged (not shown):

public class Person:System.Object, IDataErrorInfo, IEditableObject,
INotifyPropertyChanged
{
private System.Int32 PersonID;
private System.String PersonName;
private TelephoneNumbersCollection TelephoneNumbersCollection;

//code removed for brevity

}

But notice that Person contains a collection for telephone numbers which
based on <List>TelephoneNumber (not shown) i.e.
public class TelephoneNumbersCollection:List<TelephoneNumber>

For the first level, everything works fine. I can create a collection of
type <List>Person and pass it to the PersonCollectionBindingListView
constructor. When I bind to the grid this works fine.

But I am stuck with what to do about the collection of telephone numbers. In
order to interact with the grid propertly I would need to convert the
collection to inherit from: public class BindingListView<T> : BindingList<T>,
IBindingListView

i.e.
public class TelephoneCollectionBindingListView :
BindingListView<TelephoneNumber>

Where TelephoneNumber would need to implement IDataErrorInfo,
IEditableObject, InotifyPropertyChanged as the Person class does.

But since I am passing in a collection of <List>Person, it’s telepnone
collection is based on List not BindingList.

Since I wanted to have a simple structure without all the binding interfaces
so that I can use the application as a simple component. I am wondering how
to best convert from one structure to the next.

I hope I am not asking the obvious but can anyone please help me?

Also I have tried to keep this as simple as possible, by cutting out a lot
of code, please let me know if more information is required.

Best Regards,

Steve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top