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

[Solved] Assign List<string> of Class to RadDataGrid

2 Answers 171 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Robert
Top achievements
Rank 1
Robert asked on 05 Aug 2013, 02:50 PM
I am writing to ask if it is possible to assign the attributes of a List of Strings, contained within a C# class, to the RadDataGrid;

In the requirements of our project, we are trying to not pre-define properties in a C# class - rather build the C# class dynamically, based on the results from a Json.Net call to an ASP.Net Web API, which communicates with an SQL Server database; 

this.DataGrid.ItemsSource = newListInstance - at this line, in debug mode, I can see the values in the public List<string> Attributes; however, I have been unable to successfully navigate to the level where Attibutes reside;

newListInstance is the instance of the class MyClass, and if bound to the RadDataGrid, displays two columns (Metric_Display_Name and Attributes);  The Attibutes column displays 22 rows of System.Collections.Generic.List'1[System.String]

Thanks in advance for any insight, guidance;
List<MyClass> newListInstance = new List<MyClass>();

foreach (var child in jObj.Children())
                    {
                        MyClass instance = new MyClass();
                        for (int j = 0; j < child.Count; j++)
                        {
                            instance.AddAttribute((string)child[j]);                           
                        }
                        newListInstance.Add(instance);
}


class MyClass
    {
        public string Metric_Display_Name
        {
            get;
            set;
        }
 
        public List<string> Attributes
        {
            get;
            private set;
        }
 
        public MyClass()
        {
            Attributes = new List<string>();
        }
 
        public void AddAttribute(string value)
        {
            Attributes.Add(value);
        }
 
 
 
    }

2 Answers, 1 is accepted

Sort by
0
Accepted
Rosy Topchiyska
Telerik team
answered on 06 Aug 2013, 04:03 PM
Hi Robert,

Thank you for using RadControls for Windows 8!

When the grid generates its columns it looks for all properties of the first item from the ItemsSource collections. In this case the items in the ItemsSource collection are objects of type MyClass, that has Attributes property, so the grid tries to visualize this property and you get column of System.Collections.Generic.List'1[System.String].

To generate a column, the grid needs a property to bind to, so you will need to create appropriate properties in the MyClass object. Actually, you can use a collection of dynamic objects as an ItemsSource and set dynamically properties of the items that correspond to the attributes you have. For your convenience I have prepared a runnable sample application that demonstrates how this can be achieved.

Let me know if this works for you.

Regards,
Rositsa Topchiyska
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Robert
Top achievements
Rank 1
answered on 06 Aug 2013, 08:05 PM
Rositsa,

Thank you for the feedback, and sample solution - works great;

Best regards - Rob 
Tags
Grid for XAML
Asked by
Robert
Top achievements
Rank 1
Answers by
Rosy Topchiyska
Telerik team
Robert
Top achievements
Rank 1
Share this question
or