Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [Expand]What's Installed
 [Expand]Build an Application
 [Expand]Controls and Libraries
 [Collapse]Common Features
  [Collapse]Data Binding Common Concepts
    How to: Bind GridControl to Database and Implement Master-Detail Mode at Design Time
    How to: Bind a Control to a Database at Runtime
    How to: Bind a Control to Data Created at Runtime
    How to: Bind a Control to Data in an XML File
    Traditional Data Binding Methods
    Data Source Configuration Wizard
    Data Binding Mechanism in ADO.NET
    Binding Controls to XML Data
    Binding Controls to Data Created at Runtime
    Add Unbound Data to a Data-Aware Control (XtraGrid, XtraPivotGrid)
    Binding to Excel Data Sources
    Binding to LINQ to SQL Classes
    Unbound Sources
    Binding to Entity Framework Core
    Binding to OData
    Binding to SQL Data
    Data Annotation Attributes
    How to: Add Virtual Rows Using the UnboundSource Component
    Obtaining Fields Available in Data Source
  [Expand]Data Source Wizard
  [Expand]Expressions
  [Expand]Behaviors
  [Expand]Application Appearance
  [Expand]Filtering UI Context
  [Expand]High DPI Support
  [Expand]Scaffolding Wizard
  [Expand]Formatting Values
   HTML Text Formatting
  [Expand]Menus
  [Expand]Tooltip Management
  [Expand]Saving and Restoring Layouts
   Clipboard - Copy Data and Formatting
   Version Compatibility: Default Property Values
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Binding to SQL Data

Expanded Binding via the Data Source Configuration Wizard

Expanded Online Video

The most convenient way to bind your control to a SQL data source is using the Data Source Configuration Wizard. To invoke this wizard, click the corresponding link in the control's smart-tag or click the wizard icon. The figure below illustrates how to invoke the wizard for the GridControl.

  1. The first wizard page asks you to choose the technology you would like to use. Select the 'SQL Data Connection' option and click the 'New Data Source' button on the wizard's right, as shown below.

    The message box that will appear tells you that the SqlDataSource component will be added automatically. If you need to modify the data source later, you can use the SqlDataSource's smart-tag. For now, the Data Source Configuration Wizard will automatically open the required dialog.

  2. You now proceed to another wizard that belongs to the SqlDataSource component. This wizard called 'Connection Editor' allows you to bind the component to your data. To do this, select the data connection that already exists in your project, or add a new one by selecting the related option.

  3. If you have chosen to specify the connection parameters manually, you will be directed to the next page, where you need to set a custom connection string or select the desired data source type. Depending on the data provider selected, it may be necessary to specify additional connection options (such as authentication type and database name) on this page.

    In this tutorial, the sample nwind.mdb database is used.
    Tip

    The sample Northwind database can be found in the DevExpress demos' installation folder.

  4. On the last page, you may choose to save the connection and authorization data to a configuration file.

  5. After your SqlDataSource is bound to data, you will need to extract the required fields by using SQL queries. This can be done in the next wizard that pops up automatically - 'Query Designer'. This dialog can be manually invoked by clicking the 'Manage Queries...' link in the component's smart-tag and clicking the 'Run Query Builder...' button (see the figure below).

  6. Drag the desired table from the 'Items' column to the list of used data tables and check all required data columns from this table. In the figure below, the 'Products' table is used.

    You will see SQL queries appear automatically on the dialog's right as you check new columns or add new data tables.
  7. After you have clicked the 'OK' button in the previous step, the Configuration Wizzard will automatically add the required piece of code to your project and you can run the application to see the result.

Expanded Manually Binding via the SqlDataSource Component

Note

The following approach assumes manually adding and customizing the SqlDataSource component mentioned above. This method has a few drawbacks, since you will need to perform more operations, such as dropping the component onto your form from the Visual Studio toolbox, setting several required properties of your control and manually calling the Fill method. Other than that, the two methods are identical. So unless you have a strong necessity to use this approach, we recommend using the Data Source Configuration Wizard instead.

To bind your GridControl to data with the SqlDataSource component, follow the simple steps below.

  1. Locate the SqlDataSource component on the toolbox (the 'Data and Analytics' tab) and drop it onto the form.
  2. Immediately after the component is dropped, the 'Connection Editor' dialog appears.
    Note

    To invoke this dialog manually, click the 'Configure Connection' link within the component's smart-tag.

    On the first page of this wizard, select the existing data connection from the list or choose the 'No, I'd like to specify the connection parameters myself' option.

  3. If you have chosen to specify the connection parameters manually, you will be directed to the next page, where you need to set a custom connection string or select the desired data source type. Depending on the data provider selected, it may be necessary to specify additional connection options (such as authentication type and database name) on this page.

    In this tutorial, the sample nwind.mdb database is used.
    Tip

    The sample Northwind database can be found in the DevExpress demos' installation folder.

  4. On the last page, you may choose to save the connection and authorization data to a configuration file.

  5. After your SqlDataSource is bound to data, you will need to extract the required fields by using SQL queries. This can be done in the next wizard that pops up automatically - 'Query Designer'. This dialog can be manually invoked by clicking the 'Manage Queries...' link in the component's smart-tag and clicking the 'Run Query Builder...' button (see the figure below).

  6. Drag the desired table from the 'Items' column to the list of used data tables and check all required data columns from this table. In the figure below, the 'Products' table is used.

    You will see SQL queries appear automatically on the dialog's right as you check new columns or add new data tables.
  7. You now have a SqlDataSource component bound to the required data. Next, set this component as the grid control's data source (the GridControl.DataSource property) and the Products table as its data member (the GridControl.DataMember property).

  8. The final step is calling the SqlDataSource.Fill method to retrieve your data. In the code below, the method is called on the FormLoad event.
  9. Launch your application to see the result.

Expanded Binding to SQL Data in Code

The following code illustrates how to bind the GridControl to SQL data at runtime using the SqlDataSource component.

In this example, a CustomSqlQuery is used. To learn about other query types, see the SelectQuery and StoredProcQuery classes' descriptions.

To learn about supported database engines, see the inheritance hierarchy of the DataConnectionParametersBase.

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.

How would you rate this topic?​​​​​​​