List of dynamic objects as DataSource

2 posts, 0 answers
  1. Todd
    Todd avatar
    5 posts
    Member since:
    Aug 2012

    Posted 17 Aug 2015 Link to this post

    I'm using Telerik WinControls 2014.3.1104.40 in a C# environment.

    I have a gridview that has a number of preset columns, as well as zero or more additional, user-defined columns. I need a way to load this data into the grid.

    Currently, I am trying to use C# "dynamic" objects, in hope that the FieldName property of each column will access that field of the dynamic object; but the grid does not appear to load anything from a list of dynamic objects.

    There should be data loaded into the ​cells, but they show up blank. Here is a sample of the code that is not working:

    private void LoadData()
        radGridView1.DataSource = new List<dynamic>()
    private dynamic CreateDynamicObject(int id)
        dynamic ret = new ExpandoObject();
        ret.Id = id;
        ret.Prop1 = "Property 1: " + id;
        ret.Prop2 = "Property 2: " + id;
        return ret;

    If List<dynamic> data sources are not supported, what other method would be most similar?

  2. Dimitar
    Dimitar avatar
    1940 posts

    Posted 18 Aug 2015 Link to this post

    Hi Todd,

    Thank you for writing.

    RadGridView follows the standard Windows Forms data-binding model. It can data bind to any data source implementing one of the following interfaces: IList, IListSource, IBindingList, IBindingListView. However, the bindable collection should be composed of objects that have properties which should be displayed as RadGridView's columns. You can create a list of dynamic objects and bind it to the grid, but you will not be able to display anything on the screen. This is because the grid has to refer to a property and in this case, it cannot take the properties dynamically. 

    The possible solution is to use the grid's VirtualMode and manually pass the cell values depending on their index (you will need to retrieve the values from your dynamic list manually). For example:

    public partial class Form1 : Form
        List<dynamic> list = new List<dynamic>();
        public Form1()
            dynamic employee1 = new ExpandoObject();
            employee1.Name = "John Smith";
            employee1.Position = "Engineer";
            dynamic employee2 = new ExpandoObject();
            employee2.Name = "Michael";
            employee2.Position = "Pilot";
            radGridView1.EnableGrouping = false;
            radGridView1.VirtualMode = true;
            radGridView1.CellValueNeeded += radGridView1_CellValueNeeded;
            this.Load += Form1_Load;
        void Form1_Load(object sender, EventArgs e)
            radGridView1.ColumnCount = 2;
            radGridView1.RowCount = list.Count;
        void radGridView1_CellValueNeeded(object sender, Telerik.WinControls.UI.GridViewCellValueEventArgs e)
            string value = GetValue(e.RowIndex, e.ColumnIndex) ;
            e.Value = value;
        string GetValue(int rowIndex, int colIndex)
            var expandoObject = list[rowIndex] as IDictionary<string, object>;
            if (expandoObject != null)
                if (colIndex == 0)
                    return expandoObject["Name"].ToString();
                else if (colIndex == 1)
                    return expandoObject["Position"].ToString();
            return string.Empty;

    Please let me know if there is something else I can help you with. 
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top