How to: Bind Data to RadControls for WinForms
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
This topic demonstrates how to bind Telerik RadControls for WinForms such as a RadDropDownList or RadGridView to a collection of objects.
Binding Data to RadControls for WinForms
Suppose, you have a Windows Forms application and you have created a Telerik Data Access Domain Model (in this demo, a domain model based on the SofiaCarRental database will be used).
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
{
EntitiesModel dbContext = new EntitiesModel();
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 EntitiesModel()
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.