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.
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.