Data Grid
- Aug 27, 2019
- 12 min to read
The WinForms Data Grid (GridControl) provides rich capabilities for displaying, shaping and editing data from any data source. You can choose between the following data presentation formats (Views) in the Data Grid.
- Traditional tabular format (Grid View)
- Banded tabular format (Banded Grid View and Advanced Banded Grid View).
- Cards (Layout View and Card View)
- Tiles (Tile View)
- Windows Explorer-inspired style (WinExplorer View)
https://docs.devexpress.com/WindowsForms/634/controls-and-libraries/data-grid/data-binding
Data Binding
- Oct 04, 2019
- 5 min to read
- Create a New Data Source
- Choose an Existing Data Source
- Switch to Another Data Source
- Fluent API Support
- Large Data Sources: Server and Instant Feedback Modes
- Post Data to an Underlying Data Source
#Create a New Data Source
The quickest way to set up a new data source is to utilize the Data Source Configuration Wizard.
Invoke this dialog through the Data Grid smart tag or by clicking the icon in the Grid's bottom left corner.
The Wizard guides you through the binding process to the following supported sources.
- ADO.NET Data
- SQL Data
- Excel workbooks and CSV files
- Json Data
- Entity Framework
- Entity Framework Core
- LINQ to SQL Classes
- Windows Communication Foundation (WCF) services
- Open Data v4 Services
- DevExpress ORM Tool (XPO)
- Data Created at Runtime (IList, IEnumerable, etc.)
- XML Data
- Custom data with no strongly typed data set available
The UnboundSource component allows you to mix different data source types or add virtual rows to a bound Data Grid.
For code-first data sources, it is possible to mark data class properties with Data Annotation Attributes to pre-customize a Grid (e.g., skip adding a column for a specific data field or change the type of its in-place editor).
- Data Annotation Attributes
- Tutorial: Create and Manage Data in Code and Apply Data Annotation Attributes
#Choose an Existing Data Source
If you already have a data source ready, use the Data Grid smart tag to select this source in the "Choose Data Source" editor.
In code, assign a valid source to the GridControl.DataSource property.
using System.Data.OleDb;
// ...
// Create a connection object.
OleDbConnection connection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\DBs\\NWIND.MDB");
// Create a data adapter.
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Products", connection);
// Create and fill a dataset.
DataSet sourceDataSet = new DataSet();
adapter.Fill(sourceDataSet);
// Specify the data source for the grid control.
gridControl1.DataSource = sourceDataSet.Tables[0];
#Switch to Another Data Source
Once you have chosen a data source for the first time, the Data Grid automatically generates all required columns. Should you choose another data source later, previously created columns will remain and you'll need to update them manually. To do so, launch the Data Grid Designer, switch to the "Columns" tab and click the "Retrieve Fields" button.
#Post Changes to a Source
For most data source types, the Data Grid allows end-users to edit data at runtime, but these changes are not automatically saved to an underlying source. Refer to the Post Data to an Underlying Data Source article to learn how to support saving edits.
#Fluent API Support
The traditional approach to customize grid columns or bind them to data assumes that you will be accessing columns using their string field names. If for any reason, it is impossible to populate grid columns at runtime or you need custom logic to obtain column settings, you can use fluent API instead. In this approach, the Visual Studio's IntelliSense allows you to observe all the data source properties and bind the required fields to grid columns.
This approach creates a fail-safe code when modifying the data source structure (e.g., removing a data source field) or a typo in a field name (attempts to access a field that does not exist) immediately causes an exception that cannot be missed. The traditional approach can ignore such errors in certain cases, creating 'dead' code that is hard to detect.
To use this functionality, refer to the XtraGrid.Extension namespace from your code.
using DevExpress.XtraGrid.Extensions;
Now, you can call the ColumnView.With method to add and configure columns. The following code snippet illustrates an example.
gridView1.With<Customer>(settings => {
settings.Columns
.Add(f => f.ContactName, col => {
col.Caption = "Contact";
col.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
col.SortIndex = 0;
})
.Add((f => f.Phone), c => { c.Caption = "Phone Number"; })
.Add(p => p.CompanyName)
.WithCaption("Company Name")
.WithGrouping()
.With(col => { });
});
With fluent API, you are also able to obtain existing columns and tweak their settings. To do so, use the ColumnView.GetColumnViewSettings method. For instance, the following code is the GridView.RowCellStyle event handler that identifies the column related to the row cell and fills it in red if the column is 'Phone'.
void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) {
GridColumn col = (sender as ColumnView).GetColumnViewSettings<Customer>().Columns[c => c.Phone].AsColumn();
if((col!=null) && (e.Column == col)) {
e.Appearance.BackColor = Color.Red;
}
}
SEE ALSO
We are updating the DevExpress product documentation website and this page is part of our new experience. During this transition period, product documentation remains available in our previous format at documentation.devexpress.com. Learn More...
Examples: Data Binding
Examples
- How to: Bind GridControl to Database and Implement Master-Detail Mode at Design Time
- How to: Bind a GridControl to a Database in Server Mode
- How to: Bind a Control to a Database at Runtime
- How to: Bind a Control to Data Created at Runtime
- How to: Add an Unbound Column Storing Arbitrary Data
- How to: Filter a LookUp(ComboBox) Column Based on Another Column Value
- How to: Reload Data from the Bound Database in Server Mode (XPO)
- How to: Create and Customize a GridLookUpEdit Control at Runtime
- How to: Create and customize a custom GridLookUpEdit control
#Knowledge Base Articles
- How to filter the second LookUp column based on the first LookUp column's value
- Can I use the Grid Control in unbound mode?
- How to Make Sure That All Modifications Are Saved
- What is the best way to refresh the on-screen data if the datasource is an ArrayList?
- Can the grid display data from several data tables in a single Grid View?
- How to display a self-referenced data table via master and detail Grid Views
- How to synchronize a data bound control with a detail Grid View
- How to clear the grid
SEE ALSO