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

Grid with dynamic columns

2 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrey
Top achievements
Rank 1
Andrey asked on 06 Feb 2012, 08:20 PM
Hello, I'm trying to create grid columns types of which I don't know at runtime.
For that I'm using (GridElement) class with Dictionary property:
public class GridElement
    {
        Dictionary<string, object> properties = new Dictionary<string, object>();
 
        public object this[string name]
        {
            get
            {
                if (properties.ContainsKey(name))
                    return properties[name];
 
                return null;
            }
            set
            {
                properties[name] = value;
            }
        }
 
        public Dictionary<string, object> Properties
        {
            get { return properties; }
            set { properties = value; }
        }
    }

I'd like to do something like that..
List<GridElement> elementList= new List<GridElement>();
 
//Adding some data to elementList
 
//trying to generate columns
  foreach (var element in elementList[0].Properties)
            {
                GridBoundColumn column = new GridBoundColumn();
                this.TestRadGrid.MasterTableView.Columns.Add(column);
                column.HeaderText = element.Key.ToString();
                column.DataField = ???
            }
 
  this.TestRadGrid.MasterTableView.DataSource = elementList;
Any suggestions how can i make this work?

2 Answers, 1 is accepted

Sort by
0
Kshitiz
Top achievements
Rank 1
answered on 06 Feb 2012, 09:47 PM
even i have the same telerik demos are not helpful for knowledge they are 2 muhc limited :|
0
Radoslav
Telerik team
answered on 09 Feb 2012, 10:44 AM
Hi Kshitiz,

The RadGrid does not support binding to objects which contains data into the dictionary collection. To achieve the desired functionality you need to have properties for all columns into the object and bind the grid to collection of these objects. Please check out the following online examples which demonstrate the supported data sources for RadGrid binding:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/binding/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/dynamictypes/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/needdatasource/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/net2datasourcecontrols/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/databinding/net35datasourcecontrols/defaultcs.aspx

In your case you need to have properties into the GridElement class to which the columns will be bound.

Additionally please check out following documentation article which explains how to create RadGrid programmatically:
http://www.telerik.com/help/aspnet-ajax/grid-programmatic-creation.html

I hope this helps.

All the best,
Radoslav
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Grid
Asked by
Andrey
Top achievements
Rank 1
Answers by
Kshitiz
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or