Can I bind List of Dictionary<string,object> to RadGridView?

13 posts, 1 answers
  1. Tuan
    Tuan avatar
    5 posts
    Member since:
    Apr 2016

    Posted 14 Apr Link to this post

    Dear sirs,

    I have data source in format of List<Dictionary<string,object>>. Can I bind the data to RadGridView.DataSource?

    If it is not, can I extend some classes to do that?

    Thanks for your sharing.

    Tuan

  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 14 Apr Link to this post

    Hello Tuan,

    Please refer to the following article which explains how to work list Lists and RadGridView: http://docs.telerik.com/devtools/winforms/gridview/populating-with-data/binding-to-generic-lists

    In addition, you may have a look at the entire section which concerns the different ways to populate RadGridView with data.

    I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.

    Regards,
    Stefan
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Tuan
    Tuan avatar
    5 posts
    Member since:
    Apr 2016

    Posted 14 Apr in reply to Stefan Link to this post

    Dear Stefan,

    I read the online document but it is not in the case.

    I would like to describe more detail: objects is Dictionary<string, object>, for ex:

    row1 (Dictionary) : ID = 1, Name = ABC, Age = 20 

    row2 (Dictionary): ID = 2, Name = XYZ, Age = 23

    .....

    I try to set grid.DataSource = new List<Dictionary<string, object>>() { row1, row2, ...}. The Grid show corrected number of lines but has no column data.

    How can I bind the data source in this case? (grid is in read or edit mode).

    Regards,

    Tuan

  5. Dimitar
    Admin
    Dimitar avatar
    1408 posts

    Posted 14 Apr Link to this post

    Hello Tuan,

    Thank you for writing.

    If each dictionary contains only one entry there is no point to add the dictionary objects to a list. You can directly bind the grid to the dictionary. For example:
    var data = new Dictionary<string, MyObj>();
     
    for (int i = 0; i < 20; i++)
    {
        data.Add(i.ToString(), new MyObj { Name = "Name " + i, Age = 30 });
    }
    radGridView1.DataSource = data;

    This way the grid will display the Key and the Value in each row (those are the properties of the dictionary entry object). 

    If you want to display you custom business object properties you should bind the grid to a list of objects. For example:
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
              
            var data = new List<MyObj>();
     
            for (int i = 0; i < 20; i++)
            {
                data.Add( new MyObj { Name = "Name " + i, Age = 30 });
            }
            radGridView1.DataSource = data;
        }
     
    }
    class MyObj
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    If this does not help could you please share the exact way that you are initializing your data structure (use some sample data), and perhaps you can specify how exactly you expect the grid to look like.

    I am looking forward to your reply.

    Regards,
    Dimitar
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  6. Tuan
    Tuan avatar
    5 posts
    Member since:
    Apr 2016

    Posted 15 Apr Link to this post

    Dear Dimitar,


    I got your response in mailbox but it had not been in the forum.
    I had tried as your sample. But i'm sorry it is not in my situation. Because my object has dynamic properties belong to each customer site, so i can't use the strong type object (as MyObj). That is the reason i used Dictionary to store the properties.
    So, can i customize or extend the grid?


    Regards,
    Tuan
    p/s: I am evaluate the Telerik control to decide replacing ObjectListView (opensource) in my project.
  7. Dimitar
    Admin
    Dimitar avatar
    1408 posts

    Posted 18 Apr Link to this post

    Hi Tuan,

    Can you give some sample data so I can see how it is built? Can you show me how this looks in your current control?  This information will give me a better understanding of your exact requirement and I will see if this is possible with our grid. 

    I am looking forward to your reply.

    Regards,
    Dimitar
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  8. Tuan
    Tuan avatar
    5 posts
    Member since:
    Apr 2016

    Posted 18 Apr in reply to Dimitar Link to this post

    Dear Dimitar,

    I add sample code as below.

    Thank for your reply.

    Regards,

    Tuan

    ------------------

    private void InitializeGrid()
            {
                // Turn on the column auto generation, depend on return data from service.
                this.radGridView1.AutoGenerateColumns = true;

                // Setup grid's data. This is just simulation the return data from WebAPI service
                //BindingList<Dictionary<string, object>> models = new BindingList(service.GetPersonalData())
                BindingList<Dictionary<string,object>> models = new BindingList<Dictionary<string, object>>();

                string[] names = new string[] { "ROOT", "Mark", "Piet", "Herre", "Ronald", "John", "Michael" };
                Random rand = new Random();

                for (int i = 0; i < 1000; i++)
                {
                    Dictionary<string, object> row = new Dictionary<string, object>();

                    row["Id"] = i + 1;
                    row["Name"] = names[rand.Next(names.Length)];
                    row["Age"] = rand.Next(100);                
                    row["Active"] = false;

                    models.Add(row);
                }

                // Bind return data to grid
                this.radGridView1.DataSource = models;
            }

  9. Answer
    Dimitar
    Admin
    Dimitar avatar
    1408 posts

    Posted 19 Apr Link to this post

    Hi Tuan,

    Thank you for writing back.

    There is no way to directly display such data in RadGridView, however, you can use RadVirtualGrid for this case. I have attached a small sample to show you how this works. More information about this control is available in the following articles:
    Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  10. Tuan
    Tuan avatar
    5 posts
    Member since:
    Apr 2016

    Posted 19 Apr in reply to Dimitar Link to this post

    Thank you so much for your support.

    I confirmed the sample. I will investigate the RadVirtualGrid for editing mode, performance and other features.

    Regards,

    Tuan

  11. Dimitar
    Admin
    Dimitar avatar
    1408 posts

    Posted 20 Apr Link to this post

    Hi Tuan,

    I am glad I could be of help. Let us know if you have any other questions.

    Regards,
    Dimitar
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  12. Abhishek
    Abhishek avatar
    2 posts
    Member since:
    Dec 2016

    Posted 4 days and 8 hours ago Link to this post

    Can I bind a Dictionary<key,Value> that has more than one key and value from a JavaScript or Jquery At Client Side??? How???
  13. Abhishek
    Abhishek avatar
    2 posts
    Member since:
    Dec 2016

    Posted 4 days and 8 hours ago Link to this post

    Dear Sir,

    Actually I have Radgrid that is bind dynamically with a Sql Database on a Serverside but now i want to change the datasource(a dictionary<key,Value> more than one key and value) at client side using jquery or javascript.

    so i want to know is it possible to do this ?? 

    and how ?can you please tell me ?

  14. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    711 posts

    Posted 4 days and 1 hour ago Link to this post

    Hi Abhishek,

    Thank you for writing.

    This thread is discussing RadGridView for WinForms. In case you are using a different product and technology please post your question in the relevant forum.

    Regarding the grid in WinForms please refer to the reply of my colleague above: http://www.telerik.com/forums/can-i-bind-list-of-dictionary-string-object-to-radgridview#2B2FMb14SkSb-Xkqzv6WeQ.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
Back to Top
UI for WinForms is Visual Studio 2017 Ready