This is a migrated thread and some comments may be shown as answers.

Populating columns with child objects contained in a List<>

1 Answer 572 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 02 Jul 2014, 06:13 PM
I have objects which contain lists of dynamic and unique Child objects.  I would like to pivot the Child objects onto the grid so they display as one row.  I have the grid working but I can't quite get the syntax for the binding.

I have an object model like the following:

public class ParentObject
string Name
long ObjectId
List<Child> Children {get; set]

public class Child
string ChildName
string ChildValue

My grid is defined as:
<telerik:RadGridView  x:Name="myGrid" Grid.Row="0" VerticalContentAlignment="Stretch" AutoGenerateColumns="False">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" />
<telerik:GridViewDataColumn Header="Object Id" DataMemberBinding="{Binding ObjectId}" />
</telerik:RadGridView.Columns>

In the code behind I dynamically add every possible Child.Name to the grid as new columns.
List<Child> allChildren = _dataAccess.GetChildren();
 foreach (Child c in allChildren)
 {
                myGrid.Columns.Add(
                    new GridViewBoundColumnBase 
                    {
                      //DataMemberBinding = new Binding("Children[0].Value"),   //this does display the child value but puts it in every column.  I can't hardcode the 0
                        //DataMemberBinding = new Binding("Children[\"" + c.ChildName.Replace(" ","") + "\"].ChildValue"),   //tried by child Name but it doesn't bind.  To do this I updated my List so it was searchable by Name. 
                        //DataMemberBinding = new Binding("Children.ChildValue"),   Does not work
                        Name = c.Name.Replace(" ", ""),
                        Header = c.Name
                    });
    }
All of the correct columns display, I just need to know if it is possible to do a match on the ParentObject.Children.Name to a column so the grid can display the ChildValue field.
A Linq statement similar to Where(x => x.name = column name) I would think would work if that is possible in a Binding.








1 Answer, 1 is accepted

Sort by
0
Boris
Telerik team
answered on 04 Jul 2014, 06:03 AM
Hello Kevin,

I am not entirely sure what you are trying to achieve. In order to understand your scenario better could you give us an example that describes what kind of data do you have and how you need it to be displayed ?

As for the binding DataMemberBinding property of the GridViewBoundColumnBase to the Children collection, you will need to iterate through the Child items to get different values.

this.myGrid.ItemsSource = this.ParentObjects;
for (int index = 0; index < this.ParentObjects[0].Children.Count; index++)
{
    Child child = this.ParentObjects[0].Children[index];
    myGrid.Columns.Add(
            new GridViewBoundColumnBase
            {
                Name = child.ChildName.Replace(" ", ""),
                Header = child.ChildName,
                DataMemberBinding = new Binding("Children[" + index + "].ChildValue")
            });
}

We are looking forward to your reply.

Regards,
Boris Penev
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Kevin
Top achievements
Rank 1
Answers by
Boris
Telerik team
Share this question
or