Edit this page

Loading Data from RIA Services

The purpose of this tutorial is to show you how to populate a RadGridView with data from a .NET RIA Service.

This tutorial will use the Northwind database, which can be download it from here.

  • Add a new RadGridView declaration in your XAML:

<telerik:RadGridView x:Name="radGridView" Margin="8"/>

The RadGridView will be bound to a data source object, that has a property Customers. When the control is loaded all customers from the Customers table in the Northwind database are loaded asynchronously.
  • Create a new class named NorthwindDataSource.

public class NorthwindDataSource
{
}
Public Class NorthwindDataSource
End Class
  • In the NorthwindDataSource class add a reference to an ObservableCollection of Customers.

  • In the NorthwindDataSource class add a reference to your RIA Service context:

public class NorthwindDataSource
{
    SampleRiaContext riaContext;
    public NorthwindDataSource()
    {
        riaContext = new SampleRiaContext();
        this.Customers = new ObservableCollection<Customer>();
    }
    public ObservableCollection<Customer> Customers
    {
        get;
        set;
    }
}
Public Class NorthwindDataSource
    Private riaContext As SampleRiaContext
    Public Sub New()
        riaContext = New SampleRiaContext()
        Me.Customers = New ObservableCollection(Of Customer)()
    End Sub
    Public Property Customers() As ObservableCollection(Of Customer)
End Class
  • Add the following code in the constructor of the NorthwindDataSource. It will make the initial load of all Customers from the database:

LoadOperation<Customer> loadOperation = riaContext.Load<Customer>(riaContext.GetCustomersQuery());
loadOperation.Completed += loadOperation_Completed;
Dim loadOperation As LoadOperation(Of Customer) = riaContext.Load(Of Customer)(riaContext.GetCustomersQuery())
AddHandler loadOperation.Completed, AddressOf loadOperation_Completed
'

And here is the code handling the Completed event of the load operation:

private void loadOperation_Completed(object sender, EventArgs e)
{
    //Consume the result
    foreach (Customer c in riaContext.Customers)
    {
        this.Customers.Add(c);
    }
}
Private Sub loadOperation_Completed(ByVal sender As Object, ByVal e As EventArgs)
    'Consume the result
    For Each c As Customer In riaContext.Customers
        Me.Customers.Add(c)
    Next c
End Sub
  • Declare the NorthwindDataSource object as a resource in your application.

<UserControl.Resources>
    <my:NorthwindDataSource x:Key="DataSource"/>
</UserControl.Resources>
  • Update your RadGridView declaration - set the ItemsSource property.

<telerik:RadGridView ItemsSource="{Binding Source={StaticResource DataSource}, Path=Customers}"/>

Run your demo, the result can be seen on the next picture:

If you need to define the columns manually read the topic Defining Columns.

See Also