Configure the Data Bindings

This article is split into the following sections:

Setting the ItemsSource

As a typical data control RadGridView displays its data by binding to a source and generating visual elements for each item in it. The first thing you have to do is to set the ItemsSource property of RadGridView as shown in Example 1.

Example 1: Set RadGridView's ItemsSource

// In this example, GetData() is a static method of the SampleData class that returns a collection of items. 
this.radGridView.ItemsSource = SampleData.GetData(); 
' In this example, GetData() is a static method of the SampleData class that returns a collection of items. 
Me.radGridView.ItemsSource = SampleData.GetData() 

Types of Sources

Unlike a standard items control, RadGridView's ItemsSource property is declared to be of type System.Object. Of course, standard .NET collections that implement the IEnumerable interface are fully supported as well.

When binding the ItemsSource to a DataTable, you should use its DefaultView property.

To avoid coupling in your code, it may be more convenient to manipulate data in the original source collection instead of using RadGridView's API. RadGridView listens for collection change events and reflects those changes in its visual representation. In Silverlight there is a built-in implementation of a data collection that exposes the INotifyCollectionChanged interface – the ObservableCollection<T> class.

Example 2: Set RadGridView's ItemsSource to an ObservableCollection

var players = new ObservableCollection<Player>(); 
players.Add(new Player() { Name = "Pepe Reina", Number = 25 }); 
players.Add(new Player() { Name = "Jamie Carragher", Number = 23 }); 
players.Add(new Player() { Name = "Steven Gerrard", Number = 8 }); 
players.Add(new Player() { Name = "Fernando Torres", Number = 9 }); 
this.playersGrid.ItemsSource = players; 
Dim players = New ObservableCollection(Of Player)() 
players.Add(New Player() With { 
    .Name = "Pepe Reina", 
    .Number = 25 
players.Add(New Player() With { 
    .Name = "Jamie Carragher", 
    .Number = 23 
players.Add(New Player() With { 
    .Name = "Steven Gerrard", 
    .Number = 8 
players.Add(New Player() With { 
    .Name = "Fernando Torres", 
    .Number = 9 
Me.playersGrid.ItemsSource = players 

Consider using an ObservableCollection<T> or one of the other existing collection classes like List, Collection, instead of implementing your own collection. If the scenario requires a custom collection to be implemented, use the IList interface, which provides individual access by index to its items and the best performance.

Implementations of the System.ComponentModel.ICollectionView interface are fully supported as well. When such a source is provided, RadGridView will automatically pick up group descriptions, sort descriptions, or filter settings defined on the collection view and use those to display data.

Binding the Columns

The data in RadGridView is separated in columns. There are different types of columns, displaying different types of data. Usually the data is displayed in GridViewDataColumns, which can be generated automatically or manually.

Example 3: Define a column in XAML

<telerik:RadGridView x:Name="playersGrid" AutoGenerateColumns="False"> 
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="Name" /> 
        <telerik:GridViewDataColumn DataMemberBinding="{Binding Number}" Header="Number" /> 

Figure 1: The rendered RadGridView

Telerik Silverlight DataGrid ObservableCollection ItemsSource

To learn more about defining columns and the different types of columns, take a look at the Columns section.

See Also

In this article
Not finding the help you need? Improve this article