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

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

12 Answers 1966 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tuan
Top achievements
Rank 1
Tuan asked on 14 Apr 2016, 10:15 AM

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

12 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 14 Apr 2016, 11:19 AM
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.
0
Tuan
Top achievements
Rank 1
answered on 14 Apr 2016, 01:54 PM

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

0
Dimitar
Telerik team
answered on 15 Apr 2016, 08:10 AM
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.
0
Tuan
Top achievements
Rank 1
answered on 15 Apr 2016, 05:47 PM
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.
0
Dimitar
Telerik team
answered on 18 Apr 2016, 08:09 AM
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.
0
Tuan
Top achievements
Rank 1
answered on 19 Apr 2016, 04:32 AM

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;
        }

0
Accepted
Dimitar
Telerik team
answered on 19 Apr 2016, 09:47 AM
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.
0
Tuan
Top achievements
Rank 1
answered on 20 Apr 2016, 04:12 AM

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

0
Dimitar
Telerik team
answered on 20 Apr 2016, 09:23 AM
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.
0
Abhishek
Top achievements
Rank 1
answered on 01 Dec 2016, 06:45 AM
Can I bind a Dictionary<key,Value> that has more than one key and value from a JavaScript or Jquery At Client Side??? How???
0
Abhishek
Top achievements
Rank 1
answered on 01 Dec 2016, 06:49 AM

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 ?

0
Hristo
Telerik team
answered on 01 Dec 2016, 02:00 PM
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.
Tags
GridView
Asked by
Tuan
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Tuan
Top achievements
Rank 1
Dimitar
Telerik team
Abhishek
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or