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

Databinding Indexers

6 Answers 40 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Taylor
Top achievements
Rank 1
Taylor asked on 07 Apr 2011, 05:39 AM
Hi,

I have been looking to see if there is some built-in support for databinding the RadGridView to indexers.  Any links to examples/samples would be greatly appreciated.  In my particular case, I have:

class A, which has a several properties and a class B Property
class B, has several properties and indexer access by string and index

I would like to bind the RadGridView so for each row in the grid, I can see the underlying:
A properties
B's properties (the B instance accessed thru one of A's properties)
see select B values returned by B's indexer

Any assistance would be greatly appreciated.

6 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Apr 2011, 08:39 AM
Hello Taylor,

Just to be clear, by indexers you mean dictionary, hashtable & so on?
If yes, then no, you cannot do it like this, but you can use lists & autogenerate hierarchy.
Please take a look at the following example:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    private List<User> users;
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.Dock = DockStyle.Fill;
 
        users = new List<User>();
        for (int i = 0; i < 10; i++)
        {
            var user = new User
            {
                Id = i,
                Name = "Name" + i,
            };
            users.Add(user);
            user.Addresses = new List<Address>();
            user.Addresses.Add(new Address
            {
                AddressLine = "Some addressLine"
            });
 
            if (i % 2 == 0)
            {
                user.Date = DateTime.Now.AddMonths(i);
            }
        }
 
        radGridView1.AutoGenerateHierarchy = true;
        radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
        radGridView1.DataSource = users;
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        if (radGridView1.Templates.Count != 0)
        {
            radGridView1.Templates[0].AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }
    }
}
 
public class User
{
    public decimal Id
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public List<Address> Addresses
    {
        get;
        set;
    }
 
    public DateTime? Date
    {
        get;
        set;
    }
}
 
public class Address
{
    public string AddressLine
    {
        get;
        set;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Taylor
Top achievements
Rank 1
answered on 07 Apr 2011, 09:54 AM
In running your example, the values for the Addresses column are consistently System.Collections.Generic.List`1[TestSample.Address] .  Is something not listed in your example required?
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Apr 2011, 11:14 AM
Hello again Taylor,

I don't really understand what you mean, i've just created a list of addresses, the address class that has just 1 property and it is added at the end of my example.
I just wanted to illustrate the fact that the grid creates hierarchy automatically if you have list properties inside your object.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Taylor
Top achievements
Rank 1
answered on 07 Apr 2011, 08:59 PM
I was pointing out the the values that were returned running your sample.  In your example, you provided the value "Some addressLine" during your Address iniitialization. I would assume that your sample should not be displaying the text 'System.Collections.Generic.List`1[TestSample.Address]' for the Address column's value in the grid but 'Some addressline".  The Address's column is displaying the underlying object type as if it has not been correctly bound to a specific property of the Address object.

I am also trying to relate your answer to my original question. You said the grid can't be mapped to class indexers but it can be mapped to lists.  In order to solve my problem, each Address instance in your User.Addresses list would have to be displayed in their own column.  Using your class hierarchy, the results I am looking for would be able to accomplish a datagrid with columns for:

User.Id, User.Name, User.Addresses[0].AddressLine,User.Addresses[1].AddressLine,User.Date
0
Taylor
Top achievements
Rank 1
answered on 07 Apr 2011, 09:00 PM
(Accidently duplicated post - Deleted)
0
Julian Benkov
Telerik team
answered on 12 Apr 2011, 04:34 PM
Hello Taylor,

For this scenario you can use the sub-properties binding feature of RadGridView control. More information you can find in our online documentation.

I hope this was helpful.

Greetings,
Julian Benkov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Taylor
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Taylor
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or