Data Binding Common Concepts
- 4 minutes to read
Data-aware DevExpress .NET controls use the common Microsoft .NET Framework data binding mechanism. To bind a data-aware control to data, you first need a data source - an object that retrieves data from its actual storage and passes it to a control.
DevExpress data-aware controls show data from their sources as is. If you need to filter or sort records, or merge multiple data sources into a single source, this should be done at the data source level before you bind the data-aware control to this source.
Data Sources
If a data source can return data as an object that implements the IList, IBindingList, or ITypedList interface, you can bind a DevExpress data-aware control to this source. For instance, the Entity Framework data access technology utilizes System.Data.Entity.DbContext
objects as sources. This source can return data loaded from a storage as a BindingList:
Since you can bind DevExpress data-aware controls to DbContext sources, this means that controls can display Entity Framework data.
Update Data-Aware Controls When Data Storages Change
DevExpress data-aware controls cannot track data storage changes, and do not automatically update their data. Two common techniques to update a data-aware control are as follows:
- Create a “Refresh” button that relies on the data source API to reload data.
- Add the
System.Windows.Forms.Timer
component that uses the data source API to reload data whenever theTimer.Tick
event fires.
Depending on the data access technology and/or data source type, there are also other techniques. For instance, for SQL databases, you can use the SqlDependency
component and its OnDependencyChange
event. See the following Microsoft help topic for more information: Detecting Changes with SqlDependency.
Post Edited Data to Underlying Data Storages
DevExpress data-aware controls save edited data only to their sources. These changes are lost when the application restarts. To save them permanently, use the related data source API.
For example, if a control is bound to a DataSet source that uses a DataAdapter to load data, use the adapter’s Update
method.
Data sources in Entity Framework and its technologies (DbContext
objects) use the SaveChanges
method to post changes.
You can post changes at any time, but typically you should save user edits during the following events:
- When a form closes - handle the Form.Closing event.
- When a user interacts with a designated UI element - handle the
Button.Click
or a similar event. - Immediately after a user edits a cell value - handle the events indicated in the control’s corresponding help topic. For example, the following help topic explains how to save Data Grid control edits: Post Data to an Underlying Data Source.
Data Source Configuration Wizard
The Data Source Configuration Wizard is a DevExpress design-time tool that allows you to bind data-aware controls without a single line of code.
Supported Technologies and Data Sources
The list of supported data sources and data access technologies includes the following:
- Traditional Data Sources
- ADO.NET
- Azure SQL Databases
- Excel Data
- Entity Framework
- Entity Framework Core
- JSON Data
- LINQ to SQL Classes
- OData v2 and v4
- SQL Data
- WCF Data Services (Pivot Grid example)
- XML Data
- XPO Sources (e.g., XPCollection, XPBindingSource )
To bind a control to data created at runtime, you can utilize one of the following two approaches:
Cheat Sheets and Best Practices
DevExpress WinForms UI controls share the same data binding techniques. Read the following quick-reference guides for detailed information and examples:
- Supported Data Access Technologies and Data Sources
- Display Data From a Large Database or Data Storage
- Show Values from External Sources. Calculated Field Values. Unbound Mode.
- Master-Detail Mode in WinForms Data Grid
- How to Update a Database
- Update and Load Data from non UI Threads. Deal with Frequently Updated Data
Troubleshooting
Data binding issues are not usually related to DevExpress UI components directly. Read the following article for information on how to fix known issues:
Data Binding - DevExpress WinForms Troubleshooting
See Also
Examples
- How to: Add Unbound Data to a Data-Aware Control
- How to: Bind GridControl to a Database and Implement Master-Detail Mode at Design Time
- How to: Bind a Control to a Database at Runtime
- How to: Bind a Control to Data Created at Runtime
- How to: Bind a Control to Data in an XML File
- How to: Add Virtual Rows Using the UnboundSource Component