DataGrid Binding to Complex Custom Collection

  • Thread starter Thread starter Conawapa11
  • Start date Start date
C

Conawapa11

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...
 
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
 
Back
Top