Get Started With Data Grid and Views

https://docs.devexpress.com/WindowsForms/113894/controls-and-libraries/data-grid/getting-started-with-data-grid-and-views

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

The quickest way to set up a new data source is to utilize the Data Source Configuration Wizard.

Data Source Wizzard First Page

Invoke this dialog through the Data Grid smart tag or by clicking the icon in the Grid's bottom left corner.

Grid Control - SQL Data Source - Wizard 1

The Wizard guides you through the binding process to the following supported sources.

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).

#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.

Data Grid - Binding - Choose DS

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.

Data Grid - Binding - Retrieve Fields

#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

Views

Examples: Data Binding

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.comLearn More...

Examples: Data Binding

https://docs.devexpress.com/WindowsForms/3002/controls-and-libraries/data-grid/examples/data-binding/examples-data-binding

Examples

#Knowledge Base Articles

SEE ALSO

Data Binding

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值