473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Multiple Heirarchical List Datasource in DataGrid

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
3 4707
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete Davis | last post by:
I have a Windows Forms datagrid with the datasource set to an ArrayList. The ArrayList is initially empty. I have DataGridColumn styles defined for 4 columns with widths, header titles, and...
1
by: Mike | last post by:
Hi! Been stuck on this one for a bit. Would really appreciate any help on this one. To start. I have a sql database table with the following data and design (ex) ...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
0
by: dbuchanan | last post by:
How do I make multiple DataGridTableStyles serve the same table? (I can get multiple styles for the same datagrid, but not multple styles for the same *table* on the same datagrid) I want to do...
5
by: Ron L | last post by:
I have a DataTable that is being maintained in a Model class. I also have a View class which creates a DataView from the DataTable that the Model passes it. The view class then has methods to...
1
by: Waran | last post by:
How to bind Multiple results into a datagrid. For example Assume the values for dsCustCode.Tables(0).Rows.Count = 4,and the values are "'Val1','val2','val3','val4'" in the below loop In the...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.