New to Telerik UI for WinForms? Download free 30-day trial

Binding to Sub Objects

RadGridView supports out-of-the-box binding to sub objects by intuitive and simple dot (.) syntax (specified through the FieldName property of declaratively bound columns). The example below includes a "Person" class that has three properties, one of which is a reference type "Car":

  • Name - string

  • City - string

  • Car - object Car

Follows the implementation of the Person and the Car classes:

Defining the Class and Sub Class

public class Person
{
    public string _name = "";
    public string _city = "";
    public Car _car = null;
    public Person()
    {
        this._car = new Car();
    }
    public Person(string name, string city, Car car)
    {
        this._name = name;
        this._city = city;
        this._car = car;
    }
    public string Name
    {
        get
        {
            return this._name;
        }
    }
    public string City
    {
        get
        {
            return this._city;
        }
    }
    public Car Car
    {
        get
        {
            return this._car;
        }
    }
}
public class Car
{
    string _model;
    int _year;
    public Car()
    {
    }
    public Car(string model, int year)
    {
        this._model = model;
        this._year = year;
    }
    public string Model
    {
        get
        {
            return this._model;
        }
    }
    public int Year
    {
        get
        {
            return this._year;
        }
    }
}

Lets populate a BindingList of Person with some objects and bind it to RadGridView.

Binding RadGridView to Person automatically creates three columns for all properties of the Person object. The value properties are displayed correctly, but the reference property is displayed in "dot" notation (see the third (Car) column in the screenshot below).

BindingList<Person> list = new BindingList<Person>();
list.Add(new Person("Jessy Jones", "New York", new Car("BMW", 2011)));
list.Add(new Person("Allan Trenton", "Las Vegas", new Car("Mercedes", 2011)));
list.Add(new Person("Brandon Michels", "Los Angeles", new Car("Lexus", 2010)));
list.Add(new Person("Michael Jordan", "Chicago", new Car("Toyota", 2009)));
list.Add(new Person("Dimitar Berbatov", "Manchester", new Car("Infinity", 2008)));
radGridView1.DataSource = list;

WinForms RadGridView Defining Classes

Now to setup the sub-property binding of the Car column, all you have to do is to declare in the FieldName property of the column, the name of the Car object property that you want to bind the column to (Model or Year), using the dot notation:

radGridView1.Columns[2].FieldName = "Car.Model";

The result is that the Car column is now bound to the Model property of the Car object

WinForms RadGridView Binding to Sub Objects