$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
DataGridView WinForms Control
Powerful GridView C#.NET control supports data binding to various data sources
Home > WinForms UI Controls > User Manual > Binding to Sub Objects in C#

Bind GridView C#.NET to Sub Objects

The Kettic DataGridView Component allows users of the control binding the control to subjects out of the box. This is achieved by use simple dot syntax which is specified via the FieldName property of bound grid columns declaratively. We are going through the example below to demonstrate how to bind the GridView control to sub objects. There is a Person C# class defined by three properties, of which the first property is the Name, which is string, the second property is the City, which is also string, and the third is the Car property, which is the reference type Car, sub object.

How to Bind GridView to Sub Objects

Define Class and Sub Object of Class

Before we are going to bind the GridView to the sub objects, we need to define the person C# class first. The C# code below shows the details of defining the Person class and the sub class, Car.

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

}

Bind to Sub Object of Class

After defining the Person class and the sub object of the class, Car, we can populate a BindingList of the Person C# class with some objects and bind the GridView component to these objects. Having bound the GridView to the Person C# class, three new columns for all properties of the Person C# class object will be created automatically. The C# code below shows how to bind GridView control to sub objects of the class.

BindingList<Person> list = new BindingList<Person>();
list.Add(new Person("Simon Koller", "Paris", new Car("BMW", 2013)));
list.Add(new Person("Joe Demello", "London", new Car("Mercedes", 2013)));
list.Add(new Person("Ross Locker", "Washington DC", new Car("Lexus", 2012)));
list.Add(new Person("David Bowie", "New York", new Car("Toyota", 2012)));
list.Add(new Person("Ezra Legg", "Berlin", new Car("Mercedes", 2012)));

ketticGridView1.DataSource = list;
After finishing the above step, we can customize the sub property binding of the Car column. We can achieve this by declaring in the FieldName property of the grid column, and the Car object property name binding to the column. The following are the simple C# code snippet to bind GridView to sub objects.

ketticGridView1.Columns[2].FieldName = "Car.Model";
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls