How to bing a collection with sub-collection to grid

Discussion in 'ASP .Net Datagrid Control' started by gzinger, Jun 23, 2005.

  1. gzinger

    gzinger Guest

    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.

    --
    http://www.zbitinc.com
    gzinger, Jun 23, 2005
    #1
    1. Advertising

  2. 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.)
    Steven Cheng[MSFT], Jun 24, 2005
    #2
    1. Advertising

  3. gzinger

    gzinger Guest

    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.
    --
    http://www.zbitinc.com


    "Steven Cheng[MSFT]" wrote:

    > 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.)
    >
    >
    gzinger, Jun 24, 2005
    #3
  4. 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.
    Jeffrey Tan[MSFT], Jun 27, 2005
    #4
  5. gzinger

    gzinger Guest

    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.

    --
    http://www.zbitinc.com


    ""Jeffrey Tan[MSFT]"" wrote:

    > 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.
    >
    >
    gzinger, Jun 27, 2005
    #5
  6. 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.
    Jeffrey Tan[MSFT], Jun 28, 2005
    #6
  7. gzinger

    gzinger Guest

    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.
    --
    http://www.zbitinc.com


    ""Jeffrey Tan[MSFT]"" wrote:

    > 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.
    >
    >
    gzinger, Jun 29, 2005
    #7
  8. 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.
    Jeffrey Tan[MSFT], Jun 29, 2005
    #8
  9. 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.
    Jeffrey Tan[MSFT], Jul 1, 2005
    #9
  10. 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.
    Jeffrey Tan[MSFT], Jul 4, 2005
    #10
  11. gzinger

    gzinger Guest

    Great!

    Thank you very much.
    --
    http://www.zbitinc.com


    ""Jeffrey Tan[MSFT]"" wrote:

    > 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.
    >
    >
    gzinger, Jul 4, 2005
    #11
  12. You are welcome :)

    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.
    Jeffrey Tan[MSFT], Jul 5, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Øyvind Isaksen
    Replies:
    1
    Views:
    960
    Øyvind Isaksen
    May 18, 2007
  2. Ben
    Replies:
    2
    Views:
    877
  3. Gaspar
    Replies:
    0
    Views:
    333
    Gaspar
    Aug 22, 2008
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,015
    Lawrence D'Oliveiro
    May 20, 2011
  5. Mark Shapiro
    Replies:
    3
    Views:
    551
    Mark Shapiro
    Oct 8, 2011
Loading...

Share This Page