DataGrid Binding to Complex Custom Collection

Discussion in 'ASP .Net' started by Conawapa11, Nov 14, 2003.

  1. Conawapa11

    Conawapa11 Guest

    I'm having trouble figuring this problem out and every example out
    there deals with simple objects within a custom collection. Take this
    example:

    public class ComplexClass
    {
    private int id;
    private InnerClass anotherOne;

    public int ID
    {
    get { return id; }
    }
    public InnerClass AnotherOne
    {
    get { return anotherOne; }
    }
    }

    public class InnerClass
    {
    private int id;
    private string name;

    public int ID
    {
    get { return id; }
    }
    public string Name
    {
    get { return name; }
    }
    }

    then I have a
    public class ComplexCustomCollection : CollectionBase
    that contains a list of ComplexClass objects.

    Now in my Custom Control, I have a DataGrid that I want to bind to
    this ComplexCustomCollection.

    When creating columns, I have something like:

    BoundColumn column = new BoundColumn();
    column.DataField = "ID";
    column.HeaderText = "ID #";
    myGrid.Columns.Add(column);

    column = new BoundColumn();
    column.DataField = "AnotherOne.Name"; // just a guess
    column.HeaderText = "Another Name";
    myGrid.Columns.Add(column);

    myGrid.DataSource = myComplexCustomCollection;
    myGrid.DataBind();


    Which, when ran results in:
    A field or property with the name 'AnotherOne.Name' was not found on
    the selected datasource.
    How can I bind to that inner class's properties?


    Any help would be much appreciated. Thanks in advance...
    Conawapa11, Nov 14, 2003
    #1
    1. Advertising

  2. Hi,

    DataGrid trys to find Property "AnotherOne.Name". But your class
    "ComplexClass" has propery "AnotherOne"!
    You have to create Property "AnotherOne_Name"

    public class ComplexClass
    {
    ...
    public string AnotherOne_Name
    {
    return anotherOne.Name;
    }
    ...
    }

    Then for each item of the myComplexCustomCollection you will be able to find
    Property "AnotherOne_Name".
    This Property will show to you name of the class "AnotherOne".
    Try to do it.

    bye

    The Best Regards,
    Web Developer
    Michael Tkachev


    "Conawapa11" <> wrote in message
    news:...
    > I'm having trouble figuring this problem out and every example out
    > there deals with simple objects within a custom collection. Take this
    > example:
    >
    > public class ComplexClass
    > {
    > private int id;
    > private InnerClass anotherOne;
    >
    > public int ID
    > {
    > get { return id; }
    > }
    > public InnerClass AnotherOne
    > {
    > get { return anotherOne; }
    > }
    > }
    >
    > public class InnerClass
    > {
    > private int id;
    > private string name;
    >
    > public int ID
    > {
    > get { return id; }
    > }
    > public string Name
    > {
    > get { return name; }
    > }
    > }
    >
    > then I have a
    > public class ComplexCustomCollection : CollectionBase
    > that contains a list of ComplexClass objects.
    >
    > Now in my Custom Control, I have a DataGrid that I want to bind to
    > this ComplexCustomCollection.
    >
    > When creating columns, I have something like:
    >
    > BoundColumn column = new BoundColumn();
    > column.DataField = "ID";
    > column.HeaderText = "ID #";
    > myGrid.Columns.Add(column);
    >
    > column = new BoundColumn();
    > column.DataField = "AnotherOne.Name"; // just a guess
    > column.HeaderText = "Another Name";
    > myGrid.Columns.Add(column);
    >
    > myGrid.DataSource = myComplexCustomCollection;
    > myGrid.DataBind();
    >
    >
    > Which, when ran results in:
    > A field or property with the name 'AnotherOne.Name' was not found on
    > the selected datasource.
    > How can I bind to that inner class's properties?
    >
    >
    > Any help would be much appreciated. Thanks in advance...
    Michael Tkachev, Nov 14, 2003
    #2
    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. Shrage Smilowitz

    Help: Data Binding to Custom Collection

    Shrage Smilowitz, Oct 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    647
    Shrage Smilowitz
    Oct 31, 2003
  2. =?Utf-8?B?dmlrdG9yOTk5MA==?=

    trouble binding a datagrid to a custom collection

    =?Utf-8?B?dmlrdG9yOTk5MA==?=, Mar 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,673
    =?Utf-8?B?WVZUMTk2OA==?=
    Mar 16, 2006
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    939
    Øyvind Isaksen
    May 18, 2007
  4. JcFx
    Replies:
    0
    Views:
    265
  5. Terry Holland
    Replies:
    9
    Views:
    202
    Steven Cheng[MSFT]
    Oct 28, 2005
Loading...

Share This Page