Back to Devexpress

Bind the WPF Data Grid to a Local Collection

wpf-404127-controls-and-libraries-data-grid-bind-to-data-bind-to-a-local-collection.md

latest7.9 KB
Original Source

Bind the WPF Data Grid to a Local Collection

  • Dec 24, 2025
  • 6 minutes to read

This topic binds the GridControl to a local collection:

Add a Data Model

Create a data model with a collection of the Customer class:

csharp
using System;
using System.Collections.ObjectModel;

namespace BindToLocalCollection {
    public class Customer {
        public string Name { get; set; }
        public string City { get; set; }
        public int Visits { get; set; }
        public DateTime? Birthday { get; set; }
    }
    public class CustomerDataModel {
        public static ObservableCollection<Customer> GetCustomers() {
            ObservableCollection<Customer> people = new ObservableCollection<Customer>();
            people.Add(new Customer() { Name = "Gregory S. Price", City = "Hong Kong", Visits = 4, Birthday = new DateTime(1980, 1, 1) });
            people.Add(new Customer() { Name = "Irma R. Marshall", City = "Madrid", Visits = 2, Birthday = new DateTime(1966, 4, 15) });
            people.Add(new Customer() { Name = "John C. Powell", City = "Los Angeles", Visits = 6, Birthday = new DateTime(1982, 3, 11) });
            people.Add(new Customer() { Name = "Christian P. Laclair", City = "London", Visits = 11, Birthday = new DateTime(1977, 12, 5) });
            people.Add(new Customer() { Name = "Karen J. Kelly", City = "Hong Kong", Visits = 8, Birthday = new DateTime(1956, 9, 5) });
            people.Add(new Customer() { Name = "Brian C. Cowling", City = "Los Angeles", Visits = 5, Birthday = new DateTime(1990, 2, 27) });
            people.Add(new Customer() { Name = "Thomas C. Dawson", City = "Madrid", Visits = 21, Birthday = new DateTime(1965, 5, 5) });
            people.Add(new Customer() { Name = "Angel M. Wilson", City = "Los Angeles", Visits = 8, Birthday = new DateTime(1987, 11, 9) });
            people.Add(new Customer() { Name = "Winston C. Smith", City = "London", Visits = 1, Birthday = new DateTime(1949, 6, 18) });
            people.Add(new Customer() { Name = "Harold S. Brandes", City = "Bangkok", Visits = 3, Birthday = new DateTime(1989, 1, 8) });
            people.Add(new Customer() { Name = "Michael S. Blevins", City = "Hong Kong", Visits = 4, Birthday = new DateTime(1972, 9, 14) });
            people.Add(new Customer() { Name = "Jan K. Sisk", City = "Bangkok", Visits = 6, Birthday = new DateTime(1989, 5, 7) });
            people.Add(new Customer() { Name = "Sidney L. Holder", City = "London", Visits = 19, Birthday = new DateTime(1971, 10, 3) });
            return people;
        }
    }
}
vb
Imports System
Imports System.Collections.ObjectModel

Namespace BindToLocalCollection
    Public Class Customer
        Public Property Name As String
        Public Property City As String
        Public Property Visits As Integer
        Public Property Birthday As DateTime?
    End Class

    Public Class CustomerDataModel
        Public Shared Function GetCustomers() As ObservableCollection(Of Customer)
            Dim people As ObservableCollection(Of Customer) = New ObservableCollection(Of Customer)()
            people.Add(New Customer() With { .Name = "Gregory S. Price", .City = "Hong Kong", .Visits = 4, .Birthday = New DateTime(1980, 1, 1) })
            people.Add(New Customer() With { .Name = "Irma R. Marshall", .City = "Madrid", .Visits = 2, .Birthday = New DateTime(1966, 4, 15) })
            people.Add(New Customer() With { .Name = "John C. Powell", .City = "Los Angeles", .Visits = 6, .Birthday = New DateTime(1982, 3, 11) })
            people.Add(New Customer() With { .Name = "Christian P. Laclair", .City = "London", .Visits = 11, .Birthday = New DateTime(1977, 12, 5) })
            people.Add(New Customer() With { .Name = "Karen J. Kelly", .City = "Hong Kong", .Visits = 8, .Birthday = New DateTime(1956, 9, 5) })
            people.Add(New Customer() With { .Name = "Brian C. Cowling", .City = "Los Angeles", .Visits = 5, .Birthday = New DateTime(1990, 2, 27) })
            people.Add(New Customer() With { .Name = "Thomas C. Dawson", .City = "Madrid", .Visits = 21, .Birthday = New DateTime(1965, 5, 5) })
            people.Add(New Customer() With { .Name = "Angel M. Wilson", .City = "Los Angeles", .Visits = 8, .Birthday = New DateTime(1987, 11, 9) })
            people.Add(New Customer() With { .Name = "Winston C. Smith", .City = "London", .Visits = 1, .Birthday = New DateTime(1949, 6, 18) })
            people.Add(New Customer() With { .Name = "Harold S. Brandes", .City = "Bangkok", .Visits = 3, .Birthday = New DateTime(1989, 1, 8) })
            people.Add(New Customer() With { .Name = "Michael S. Blevins", .City = "Hong Kong", .Visits = 4, .Birthday = New DateTime(1972, 9, 14) })
            people.Add(New Customer() With { .Name = "Jan K. Sisk", .City = "Bangkok", .Visits = 6, .Birthday = New DateTime(1989, 5, 7) })
            people.Add(New Customer() With { .Name = "Sidney L. Holder", .City = "London", .Visits = 19, .Birthday = New DateTime(1971, 10, 3) })
            Return people
        End Function
    End Class
End Namespace

Add a View Model

Create a View Model class that receives data from the CustomerDataModel :

csharp
using DevExpress.Mvvm;
using System.Collections.ObjectModel;

namespace BindToLocalCollection {
    public class ViewModel : ViewModelBase {
        public ViewModel() {
            Source = CustomerDataModel.GetCustomers();
        }
        public ObservableCollection<Customer> Source { get; }
    }
}
vb
Imports DevExpress.Mvvm
Imports System.Collections.ObjectModel

Namespace BindToLocalCollection
    Public Class ViewModel
        Inherits ViewModelBase

        Public Sub New()
            Source = CustomerDataModel.GetCustomers()
        End Sub

        Public ReadOnly Property Source As ObservableCollection(Of Customer)
    End Class
End Namespace

Bind the GridControl to Data

  1. Build the solution to make the ViewModel class visible in the window’s Quick Actions.

  2. Open the window’s Quick Actions and specify the window’s data context:

  3. Open the GridControl‘s Quick Actions and specify the ItemsSource :

  4. Run the project. The GridControl generates columns for all fields from a bound data source.

More Examples

The following example binds the WPF Data Grid to different data sources.

View Example: Bind the WPF Data Grid to Data

This example includes multiple solutions that demonstrate:

  • How to bind the Data Grid to Entity Framework, EF Core, and XPO.
  • Different binding mechanisms: virtual sources, server mode sources, and local data.
  • MVVM and code-behind patterns.

After you bind the Data Grid to a database, you can implement CRUD operations (create, read update, delete). Refer to the following topic for more information: CRUD Operations in a Data-Bound Grid.

View Example: Implement CRUD Operations in the WPF Data Grid

See Also

Lesson 1 - Add a GridControl to a Project and Bind it to Data

Bind the WPF Data Grid to a Local Database

Create Columns and Bind Them to Data Properties