windowsforms-2163-common-features-data-binding-how-to-bind-gridcontrol-to-database-and-implement-master-detail-mode-at-design-time.md
This example demonstrates how to bind a data-aware control (XtraGrid, XtraPivotGrid, XtraVerticalGrid, etc.) to a database at design time within Microsoft Visual Studio. Note that specific controls may need additional customization after the control is bound to a data source. For more information, refer to the documentation of the corresponding control.
In this example, a GridControl is bound to a database to represent master-detail relationships.
Microsoft Visual Studio introduces a BindingSource component, which simplifies the binding of data-aware controls to data. For information on this component, please refer to the BindingSource component topic in MSDN.
This topic shows how to use a BindingSource component to bind a Grid Control to the “Orders” and “Order Details” tables in the NorthWind database (the nwind.mdb file that ships with the installation of DevExpress controls). These tables are linked by a one-to-many relationship (a single row in the first table can be related to one or more rows in the second table, but a row in the second table can only be related to one row in the first table). In the example below, the grid will represent these tables as master and detail levels (Views).
After you perform the steps in this example, the result will be similar to the following.
Start Visual Studio and create a new Windows Forms Application. Drop a Grid Control on the form.
Open the Grid Control Tasks pane by clicking the Grid’s smart tag button.
Expand the Choose Data Source popup panel and click the Add Project Data Source… link.
Select Database and click Next.
Select Dataset and click Next.
Specify the path and connection settings to the NorthWind database (the nwind.mdb file) using subsequent dialogs.
The next page allows you to choose which tables need to be obtained from the database. Select the ‘Orders’ and ‘Order Details’ tables, and then click Finish.
The Grid Control should display the ‘Order’ table as the master data table. To bind to this table, click ‘Orders’ in the Choose Data Source popup panel.
If you run your application now, you will notice inactive row expansion buttons, since the ‘Order Details’ table is empty.
The next section will show you how to populate the ‘Order Details’ table data.
As you can see, Visual Studio has generated a set of classes that support the ADO.NET architecture.
To populate ‘Order Details’ table data, create another TableAdapter that will communicate with this table.
Open the Visual Studio Toolbox window and add the Order_DetailTableAdapter object onto your application form (the solution needs to be built beforehand).
Shift to application code view by pressing the F7 key. Notice that the following code already exists.
Run the solution. Master rows can now be expanded to view detail rows.
Hiding Columns
Creating a Detail View
Other Tasks
The following image illustrates the resulting grid at runtime.