How to bing a collection with sub-collection to grid

G

gzinger

I have a collection of objects (say Customers for example). Each Customer
Object has a collection of orders. When I bind such complex type to datagrid
control I see the first level fields (fields of customer class) fine and I
see a link to orders, but when I click on the link it shows no records, as I
would expect.
If I bind a DataSet with the same structure then the result is as expected.
So, I know that there is a way. I just need to find out how.
I suspect I need to implement some interface on my Orders collection. Is
that the case? Which interface would it be? Or maybe there is some attribute
that I need to front my class with? Any ideas would be greatly appreciated.

Thank you.
 
S

Steven Cheng[MSFT]

Hi Gzinger1,

Welcome to ASPNET newsgroup.
As for the binding colleciton together with sub-colleciton to asp.net
template databound controls(datagrid, datalist....) you mentioned, we can
use the hierarchical databinding functionality for those databound
controls. Here are some MSDN tech article discussing on this:

#Nested Grids for Hierarchical Data
http://msdn.microsoft.com/msdnmag/issues/03/10/CuttingEdge/default.aspx

#Hierarchical Data Binding in ASP.NET
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/aspn-hierdatabinding.asp

Also, you can find some former threads in the newsgroup discussing on the
related questions;

http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet
..datagridcontrol/browse_thread/thread/4a05cfe834211126/22540e1257c7a394?q=as
p.net+hierarchical+databinding+steven+cheng&rnum=1&hl=en#22540e1257c7a394

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

gzinger

Dear Steven,

Thank you for your prompt reply.
Unfortinatly, I didn't make myself clear. I was NOT asking about ASP.NET
grid. I was asking about Windows Forms DataGrid control. It has this nested
grid functionality avalable by default (without writing any code whatsoever).
One just binds a DataSatet , with 2 or more tables and relations between them
to the control, and it just works.
Unfortinatly, we can only make it work when binding the grid to a DataSet.
When binding the grid to a collection (not DataSet) we can see the first
level of data and we also see the links (under "+" column) to the child
collection but when the link is clicked, en empty grid is showing.
Any ideas?

Thanks.
 
J

Jeffrey Tan[MSFT]

Hi Gzinger1,

Thanks for your feedback.

Can you show us some code snippet to help us reproduce out this problem?
This will help us understand your problem better and faster. A little
sample project is also good for this.

I will wait for your further feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

gzinger

Dear Jeffrey Tan,

There is not much of a source code to show. If anything, I would think you
would want to see screen shots, but I am not sure how to attach them here.
The source code is just:

//This works as expected
myGrid.DataSource=myNestedDataSet;
myGrid.DataBind();

//This only shows first level elements - not good
myGrid.DataSource=myNestedCollection;
myGrid.DataBind();

Thanks.
 
J

Jeffrey Tan[MSFT]

Hi Gzinger1,

Thanks for your feedback.

I think the key point of the issue is how you implement your datasource. I
suggest you paste the full code snippet of your myNestedCollection.
Including the class definition and internal child class definition. This
information is key to the databinding and controls how Winform datagrid
behaves.

Currently, I have writen a sample for your issue, which works well:

public class Customer
{
private string _prop;
public string Prop
{
get
{
return _prop;
}
set
{
_prop=value;
}
}

private ArrayList _colllection;
public ArrayList Collection_Prop
{
get
{
return _colllection;
}
set
{
_colllection=value;
}
}
}

public class order
{
private string _prop;
public string Prop
{
get
{
return _prop;
}
set
{
_prop=value;
}
}
}

private void Form1_Load(object sender, System.EventArgs e)
{
Customer [] c_arr=new Customer[5];
for(int i=0;i<5;i++)
{
c_arr=new Customer();
c_arr.Prop=i.ToString();
c_arr.Collection_Prop=new ArrayList();
for(int j=0;j<5;j++)
{
c_arr.Collection_Prop.Add(new order());
((order)c_arr.Collection_Prop[j]).Prop="item"+j.ToString();
}
}

this.dataGrid1.DataSource=c_arr;
}
Note: in my words "works well", I mean that the "+" sign will appear, and
when we click the link button of the "+" sign, I can navigate to the child
collections without any problem.

Currently, I suspect you used customized DataGridTableStyle in your
DataGrid? If so, please provide more information about this. Thanks
===================================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

gzinger

Dear Jeffrey Tan,

Thank you very much. I will try that and tell you the result.

Would you happen to know wich Interface of ArrayList does the trick?

Thanks.
 
J

Jeffrey Tan[MSFT]

Hi gzinger1,

Yes, I am looking for why ArrayList did the trick now. I will spend a
little more time on it and update you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi gzinger1,

Sorry for letting you wait for so long.

Currently, I still did not find the root cause for this issue, I will spend
a little more time. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Gzinger1,

Sorry for letting you wait for so long.

After doing some research, we find that it is
DataGridTableStyle.PropertyDescriptorIsARelation method which determine the
"+" sign to appear. Below is the code for PropertyDescriptorIsARelation
method from Reflector:

private bool PropertyDescriptorIsARelation(PropertyDescriptor prop)
{
if (typeof(IList).IsAssignableFrom(prop.PropertyType))
{
return !typeof(Array).IsAssignableFrom(prop.PropertyType);
}
return false;
}

As we can see, any class implement IList can has a relation exception Array
type. It seems that this is hardcoded intention.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top