Data Access has been discontinued. Please refer to this page for more information.

How to: Bind Data to Telerik® UI for WinForms

This topic demonstrates how to bind Telerik UI for WinForms such as a RadDropDownList or RadGridView to a collection of objects.

Binding Data to Telerik UI for WinForms

Suppose, you have a Windows Forms application and you have created the SofiaCarRental model.

When the main form is loaded, a LINQ query is executed to load all Category objects from the database. This result is bound to a RadDropDownList control. When a specific Category is selected, the related collection of Car objects is bound to a RadGridView control.

The user interface of the application is pretty simple:

  • Two RadLabel controls.
  • A RadDropDownList control with Name set to radDropDownListCategories.
  • A RadGridView control with Name set to radGridViewCars.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WinFormsApplication
{
   public partial class Form1 : Form
   {
       FluentModel dbContext = new FluentModel();

       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           radDropDownListCategories.DataSource = dbContext.Categories.ToList();
           radDropDownListCategories.DisplayMember = "CategoryName";
       }

       private void radDropDownListCategories_SelectedIndexChanged(object sender,
             Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
       {
           if (radDropDownListCategories.SelectedItem != null)
           {
               Category selectedCategory = radDropDownListCategories.SelectedItem.
                    DataBoundItem as Category;
               List<Car> cars = dbContext.Cars.Where(p => p.CategoryID == 
                    selectedCategory.CategoryID).ToList();
               BindingSource bs = new BindingSource();
               bs.DataSource = cars;
               radGridViewCars.DataSource = bs;
           }
       }
   }
}
Public Class Form1
    Dim dbContext As New FluentModel()

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
        Handles MyBase.Load
        radDropDownListCategories.DataSource = dbContext.Categories.ToList()
        radDropDownListCategories.DisplayMember = "CategoryName"
    End Sub

    Private Sub radDropDownListCategories_SelectedIndexChanged(sender As System.Object, _
        e As Telerik.WinControls.UI.Data.PositionChangedEventArgs) _
        Handles radDropDownListCategories.SelectedIndexChanged
        If radDropDownListCategories.SelectedItem IsNot Nothing Then
            Dim selectedCategory As Category = TryCast
                (radDropDownListCategories.SelectedItem.DataBoundItem, Category)
            Dim cars As List(Of Car) = dbContext.Cars.Where(Function(p) p.CategoryID = 
                selectedCategory.CategoryID).ToList()
            Dim bs As New BindingSource()
            bs.DataSource = cars
            radGridViewCars.DataSource = bs
        End If
    End Sub
End Class

A sample output is shown on the snapshot below:

Next Steps

For a complete walkthrough, check out the Quickstart - WinForms section.