XtraGrid – Binding a Grid to Data

In this lesson, our main goal is to show you how to bind XtraGrid to data. Thus, I’m going to create an extremely simple application that has a single form with Grid Control bound to an access database.

1.  First, let’s drop a Grid Control onto a new form.

2.  And make it fill the entire form’s area.

3.  Now we need to create a data source and bind it to the grid.

4.  These actions can be done at once by the Grid’s smart tag.

5.  I click the “Add Project Data Source…” link to start the “Data Source Configuration Wizard”.

6.  The first step is to choose a data source type and it is “Database” in our case.

7.  Hence, I leave the default option and proceed to the next page to choose a data connection.

8.  I get to create a new connection here.

9.  All I have to do is to locate the desired database file on the disk . . . and I’m done!

10.  In the last step, I’ll choose the data table that will supply data to the grid.

11.  The “Customers” table will do nicely in this lesson.

12.  I’ll specify a meaningful name for the DataSet . . .

13.  . . . and click Finish.

14.  The wizard has been completed . . . AND . . . as you can see, I have not only created the Data Source, but also bound it to the grid.

15.  We can see that the grid has automatically retrieved all the fields from the bound table and created columns for them.

16.  Since I don’t need to display all the columns, I’ll invoke the Grid’s Designer to remove some of them.

17.  I’ll switch to the columns page, select the columns I wish to remove, and delete them with a single click.

18.  You see that the corresponding field names are now bold, indicating that there are no columns bound to them.

19.  To create a column bound to a particular field, you can simply drag an item from the Fields ListBox, to the Columns ListBox.

20.  Now I’m done with column customization, so I’ll close the designer.

21.  Finally, I can run the application to see the result.

22.  A Grid Control, filled with data from the bound Data Table.