Other DevExpress WinForms Cheat Sheets
How to update a data base
DevExpress data-aware controls (Gantt Control, Data Grid, Tree List, Scheduler, Vertical Grid etc) don't interact with an underlying database directly. Instead, they use data of corresponding data sources. Controls do not offer methods to read data from a data base or post changes to the data base. To connect these data-aware controls to a data base, you can use any standard approach.
To display data from large data bases, you can use the approach described at Display data from large data bases.
Implement the update logic at the data source layer. For instance, if you supply data to a control via a data source that implements IBindingList, you can handle IBindingList.ListChanged to post changes to the underlying data base.
To push changes to the underlying data base when your application closes or when an end-user has finished their edits, you can handle the main form's Form.Closing event or a separate Save button's Click event. In these event handlers use methods of an underlying data source to post changes.
After an end-user has modified one row and tries to navigate to another, the ColumnView.RowUpdated event fires. Handle this event to call required data source API.
RowUpdated does not fire while the editor is still active (BaseView.IsEditing returns true). To trigger this event manually, you can close this editor (BaseView.CloseEditor / PostEditor) and call the BaseView.UpdateCurrentRow method.
If a row is removed, handle ColumnView.RowDeleted.
Refer to this help article for more information: Post Data to an Underlying Data Source.
Master-Detail mode
Views that are assigned to detail Data Grid levels are patterns that store settings. These Views are never connected to real data and their data-related methods (like BaseView.CloseEditor or BaseView.UpdateCurrentRow) do not function.
When a master row expands, Data Grid dynamically creates clones of the pattern to display detail data. Data-related API should be called for these clone Views. To access a clone, use the GridView.GetDetailView method. Refer to the following section of the "Master-Detail Relationships" help article for more information: Patterns and Clones.
When an appointment is about to be modified / inserted / deleted, the Scheduler fires the following events. You can set the e.Cancel
property to true to cancel upcoming changes.
If you do not cancel appointment edits and they are applied, the following events occur. Call required data source methods inside these events' handlers to push modified appointment data to an underlying source.
How to update a control when a data base changes
DevExpress data-aware controls use the standard .NET mechanism to refresh data. Neither DevExpress data-aware controls nor DevExpress data sources expose any specific API to track changes that happen at the underlying data base layer.
To update a DevExpress control when a data base changes, use any of the following solutions:
- Place a custom Refresh button on a form and call corresponding data source API to reload data-aware control's data whenever this button is clicked.
- Use System.Windows.Forms.Timer to reload data on the Timer.Tick event.
Examples
How to commit changes to the database after a user has made changes to a row and the row loses focus?
When row properties are changed and the row loses focus, RowUpdated is raised. You can handle this event to post data to the data base using methods of the GridControl's data source.
Is it possible to save only modified cells or rows to a database?
Since DevExpress data-aware controls don't interact with an underlying database directly, you can make sure that all changes made in Data Grid are pushed to the underlying data source by invoking BaseView.CloseEditor and BaseView.UpdateCurrentRow and then use methods your data source provides to post changes to the database.
How to automatically detect data base changes?
A typical scenario: I have an SQL Data Base which several workstations are connected to. Data can be changed by these workstations - for example, a new a record can be added or an existing record can be modified. How can I keep data displayed in these workstations up to date?
Solution: Use any of the aforementioned solutions to reload data manually when SQL data base tables change. Alternatively, use the SqlDependency component to detect changes - you can load data into data-aware controls on the OnDependencyChange event. See this MSDN article for more information: Detecting Changes with SqlDependency.
How can I reflect data base changed when EntityInstantFeedbackSource is used?
Solution 1: Refresh your ObjectContext instance and GridControl's data source by calling the GridControl.RefreshDataSource method in the Timer.Tick event handler or when the Refresh button is clicked:
C#private void RefreshData() {
IObjectContextAdapter contextAdapter = dataHelper.SpaceObjectsContext as IObjectContextAdapter;
contextAdapter.ObjectContext.Refresh(System.Data.Entity.Core.Objects.RefreshMode.StoreWins, dataHelper.SpaceObjects);
gridControl.RefreshDataSource();
}
See the EF _RefreshData_1.zip sample project.
Solution 2: Disable caching in the DbContext or ObjectContext. This method works by calling the AsNoTracking method of the underlying query object. Then, use EntityInstantFeedbackSource.Refresh:
C#private void EntityInstantFeedbackSource_GetQueryable(object sender, GetQueryableEventArgs e) {
e.QueryableSource = dataHelper.SpaceObjects.AsNoTracking();
e.Tag = dataHelper.SpaceObjectsContext;
}
private void RefreshData() {
entityInstantFeedbackSource.Refresh();
}
See the EF _RefreshData_2 sample project.