By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,081 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,081 IT Pros & Developers. It's quick & easy.

Multiple Heirarchical List Datasource in DataGrid

P: n/a
Hi All,
I was trying to apply databinding to a custom collection as in the MSDN
Magazine article found in the August, 2005 issue. After converting the code
in the article to C# and applying it to an existing set of classes, I tried
using the collection as the datasource for a standard DataGrid control. The
problem is that the collection contains a collection as one of it's member
properties (ie. SampleCollection contains a SampleParameterCollection). The
DataGrid control seems to correctly detect that a heirarchy exists as it
displays the expander "[+]" and shows the name of the inner collection on an
indented line when you click the expander. However, if you click the
underlined property name link, instead of displaying the sample parameters
collection, the program hits an unhandled exception after walking the
property list of the first member of the inner collection once.

This is the error:
An unhandled exception of type 'System.Reflection.TargetInvocationException'
occurred in system.dll

Additional information: Property accessor 'SampleID' on object
'DAQ_LIMS_OBJ.CSampleParam' threw the following exception:'Object does not
match target type.'

The first question I have is: Is there some hand-off code the needs to be
in place to support Heirarchical data sets in DataGrids that wasn't covered
by the original article? The code works great with a "flat" collection.

Second question: Where can I find a book that covers how all these things
work (esp. the Framework stuff) Every book I've got covers basic
programming topics and C# language features but rarely addresses tricky stuff
like this. The online help is nearly worthless as it seems to consist only
of F1 (context) helps and random articles...where's the Programmer's guide
and reference? Where's the comprehensive framework docs?

Thanks in advance for any help you can offer.
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

"BobforeApples" <Bo***********@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi All,
I was trying to apply databinding to a custom collection as in the MSDN
Magazine article found in the August, 2005 issue. After converting the
code
in the article to C# and applying it to an existing set of classes, I
tried
using the collection as the datasource for a standard DataGrid control.
The
problem is that the collection contains a collection as one of it's member
properties (ie. SampleCollection contains a SampleParameterCollection).
The
DataGrid control seems to correctly detect that a heirarchy exists as it
displays the expander "[+]" and shows the name of the inner collection on
an
indented line when you click the expander. However, if you click the
underlined property name link, instead of displaying the sample parameters
collection, the program hits an unhandled exception after walking the
property list of the first member of the inner collection once.

This is the error:
An unhandled exception of type
'System.Reflection.TargetInvocationException'
occurred in system.dll

Additional information: Property accessor 'SampleID' on object
'DAQ_LIMS_OBJ.CSampleParam' threw the following exception:'Object does not
match target type.'

The first question I have is: Is there some hand-off code the needs to be
in place to support Heirarchical data sets in DataGrids that wasn't
covered
by the original article? The code works great with a "flat" collection.
Yes the code only deals with flat collections and the problem is the
implementation of ITypedList. ITypedList must not only return the
properties of it's own elements but also of the elements of any
subcollection at any level, the listAccessors parameter is a path of
properties to the child collection.

There are many ways to implement ITypedList depending on various things
like:
- do the items implement ICustomTypeDescriptor
- is ITypedList implemented in the same way for all collections
and so one ...

Here is an implementation of ITypedList that will work for an object
hierarchy when:
- items don't implement ICustomTypeDescriptor and
- all collections are implemented in the same way

PropertyDescriptorCollection ITypedList.GetItemProperties(
PropertyDescriptor[] listAccessors )
{
PropertyDescriptorCollection sortedList = null;
if ( listAccessors==null || listAccessors.Length==0 )
{
PropertyDescriptorCollection originalList=
TypeDescriptor.GetProperties(typeof(Student*));

sortedList = originalList.Sort(new String[] { "FirstName",
"LastName", "Age", "Grade"}*);
}
else
{
ITypedList typedList = (ITypedList)
Activator.CreateInstance(listAccessors[listAccessors.Length-1].PropertyType);
sortedList = typedList.GetItemProperties(null);
}
return sortedList;
}

string ITypedList.GetListName( PropertyDescriptor[] listAccessors )
{
if ( listAccessors==null || listAccessors.Length==0 )
{
return "StudentCollection"*;
}
else
{
ITypedList typedList = (ITypedList)
Activator.CreateInstance(listAccessors[listAccessors.Length-1].PropertyType);
return typedList.GetListName(null);
}
}

Things with a * need to be replaced with the appropriate values.

Second question: Where can I find a book that covers how all these things
work (esp. the Framework stuff) Every book I've got covers basic
programming topics and C# language features but rarely addresses tricky
stuff
like this.
I don't know you can try to find more examples maybe codeproject or learn
the individual parts and then put it together yourself, in particular you
should learn about: IBindingList, ITypedList, PropertyDescriptor,
TypeDescriptor, ICustomTypeDescriptor.

PropertyDescriptor's are important because that's what databinding directly
uses. PropertyDescriptor's can either refer to an actual property in a
class or a dynamic property ( a property that isn't defined in a class at
compile time ).

TypeDescriptor.GetProperties(type) gets a collection of PropertyDescriptors
that all refer to actual properties on the type.
The online help is nearly worthless as it seems to consist only
of F1 (context) helps and random articles...where's the Programmer's guide
and reference? Where's the comprehensive framework docs?
NET Framework SDK Documentation ?
http://www.msdn.microsoft.com/librar...kref_start.asp

HTH,
Greetings

Thanks in advance for any help you can offer.



Nov 17 '05 #2

P: n/a
BobforeApples wrote:
Hi All,
I was trying to apply databinding to a custom collection as in the
MSDN Magazine article found in the August, 2005 issue. After
converting the code in the article to C# and applying it to an
existing set of classes, I tried using the collection as the
datasource for a standard DataGrid control. The problem is that the
collection contains a collection as one of it's member properties
(ie. SampleCollection contains a SampleParameterCollection). The
DataGrid control seems to correctly detect that a heirarchy exists as
it displays the expander "[+]" and shows the name of the inner
collection on an indented line when you click the expander.
However, if you click the underlined property name link, instead of
displaying the sample parameters collection, the program hits an
unhandled exception after walking the property list of the first
member of the inner collection once.

This is the error:
An unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in system.dll

Additional information: Property accessor 'SampleID' on object
'DAQ_LIMS_OBJ.CSampleParam' threw the following exception:'Object
does not match target type.'

The first question I have is: Is there some hand-off code the needs
to be in place to support Heirarchical data sets in DataGrids that
wasn't covered by the original article? The code works great with a
"flat" collection.

Second question: Where can I find a book that covers how all these
things work (esp. the Framework stuff) Every book I've got covers
basic programming topics and C# language features but rarely
addresses tricky stuff like this. The online help is nearly
worthless as it seems to consist only of F1 (context) helps and
random articles...where's the Programmer's guide and reference?
Where's the comprehensive framework docs?


besides bart's info, here's more info on making a collection bindable
with IBindingList and ITypedList:

http://weblogs.asp.net/fbouma/articles/115837.aspx

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 17 '05 #3

P: n/a
Bart,

Thanks for the in-depth reply. I'll see if I can puzzle out whether I
implemented the collections in a manner compatible with this technique. I'm
fairly new to .NET, so I'm amazed that I got anything working at all.

..NET Framework SDK...DOH! I guess I was too focused on finding a C# manual
in the MSDN...still if anyone cares to recommend a good book on the
subject...my eyes have been around long enough to be due for replacement (or
at least rotation) & it's easier to read a book than look at a screen for
hours.

I don't have the funds to buy every book out there to find the Petzold of
the Framework books, so if anybody cares to finish this sentence:

" I you can only buy one .NET Framework book this year, it has to be
____________."

And BTW, thanks again!

"Bart Mermuys" wrote:
Hi,

"BobforeApples" <Bo***********@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi All,
I was trying to apply databinding to a custom collection as in the MSDN
Magazine article found in the August, 2005 issue. After converting the
code
in the article to C# and applying it to an existing set of classes, I
tried
using the collection as the datasource for a standard DataGrid control.
The
problem is that the collection contains a collection as one of it's member
properties (ie. SampleCollection contains a SampleParameterCollection).
The
DataGrid control seems to correctly detect that a heirarchy exists as it
displays the expander "[+]" and shows the name of the inner collection on
an
indented line when you click the expander. However, if you click the
underlined property name link, instead of displaying the sample parameters
collection, the program hits an unhandled exception after walking the
property list of the first member of the inner collection once.

This is the error:
An unhandled exception of type
'System.Reflection.TargetInvocationException'
occurred in system.dll

Additional information: Property accessor 'SampleID' on object
'DAQ_LIMS_OBJ.CSampleParam' threw the following exception:'Object does not
match target type.'

The first question I have is: Is there some hand-off code the needs to be
in place to support Heirarchical data sets in DataGrids that wasn't
covered
by the original article? The code works great with a "flat" collection.


Yes the code only deals with flat collections and the problem is the
implementation of ITypedList. ITypedList must not only return the
properties of it's own elements but also of the elements of any
subcollection at any level, the listAccessors parameter is a path of
properties to the child collection.

There are many ways to implement ITypedList depending on various things
like:
- do the items implement ICustomTypeDescriptor
- is ITypedList implemented in the same way for all collections
and so one ...

Here is an implementation of ITypedList that will work for an object
hierarchy when:
- items don't implement ICustomTypeDescriptor and
- all collections are implemented in the same way

PropertyDescriptorCollection ITypedList.GetItemProperties(
PropertyDescriptor[] listAccessors )
{
PropertyDescriptorCollection sortedList = null;
if ( listAccessors==null || listAccessors.Length==0 )
{
PropertyDescriptorCollection originalList=
TypeDescriptor.GetProperties(typeof(Student*));

sortedList = originalList.Sort(new String[] { "FirstName",
"LastName", "Age", "Grade"}*);
}
else
{
ITypedList typedList = (ITypedList)
Activator.CreateInstance(listAccessors[listAccessors.Length-1].PropertyType);
sortedList = typedList.GetItemProperties(null);
}
return sortedList;
}

string ITypedList.GetListName( PropertyDescriptor[] listAccessors )
{
if ( listAccessors==null || listAccessors.Length==0 )
{
return "StudentCollection"*;
}
else
{
ITypedList typedList = (ITypedList)
Activator.CreateInstance(listAccessors[listAccessors.Length-1].PropertyType);
return typedList.GetListName(null);
}
}

Things with a * need to be replaced with the appropriate values.

Second question: Where can I find a book that covers how all these things
work (esp. the Framework stuff) Every book I've got covers basic
programming topics and C# language features but rarely addresses tricky
stuff
like this.


I don't know you can try to find more examples maybe codeproject or learn
the individual parts and then put it together yourself, in particular you
should learn about: IBindingList, ITypedList, PropertyDescriptor,
TypeDescriptor, ICustomTypeDescriptor.

PropertyDescriptor's are important because that's what databinding directly
uses. PropertyDescriptor's can either refer to an actual property in a
class or a dynamic property ( a property that isn't defined in a class at
compile time ).

TypeDescriptor.GetProperties(type) gets a collection of PropertyDescriptors
that all refer to actual properties on the type.
The online help is nearly worthless as it seems to consist only
of F1 (context) helps and random articles...where's the Programmer's guide
and reference? Where's the comprehensive framework docs?


NET Framework SDK Documentation ?
http://www.msdn.microsoft.com/librar...kref_start.asp

HTH,
Greetings

Thanks in advance for any help you can offer.



Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.