Bind the WPF Data Grid to a Local Database
- 2 minutes to read
This topic demonstrates how to bind the GridControl to a local database:
Connect your project to a local database.
Create a ViewModel template that implements ViewModelBase:
using DevExpress.Mvvm; namespace WPFBlankAppWithDatabase { public class ViewModel : ViewModelBase { public ViewModel() { } } }
Build the solution to make the ViewModel class visible in the window’s Quick Actions.
Open the ThemedWindow‘s Quick Actions and define the window’s data context:
You can also define the window’s data context in code:
<dx:ThemedWindow ... xmlns:local="clr-namespace:DXGridGetStarted"> <dx:ThemedWindow.DataContext> <local:ViewModel/> </dx:ThemedWindow.DataContext>
Open the GridControl‘s Quick Actions and invoke the Items Source Wizard.
Select the data source.
Select the Order table.
Select the Simple Binding model.
Ensure that the CRUD options are enabled.
Select the ViewModel option to generate data binding code in the ViewModel class. Verify that the ViewModel class is selected as the View Model.
Run the project. The GridControl generates columns for all fields from a bound data source:
Note
For information on how to post changes to a database, refer to the following help topic: Post Changes to a Database.
#GitHub Example
#More Examples
The following example shows how to bind the WPF Data Grid to different data sources.
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.