A newer version of this page is available. Switch to the current version.

Binding to OData

  • 6 minutes to read

OData V4 is an open protocol initiated by Microsoft. Its RESTful API allows you to publish, read and edit resources defined in a data model using simple HTTP messages. The protocol currently includes three major versions: 2.0, 3.0 and 4.0. This document spotlights versions 4.0 and 2.0 as the most widely used versions. For version 2.0, binding by using Windows Communication Foundation (WCF) services is used.

Prerequisites

For both v4 and v2 OData sources, start with the following.

  1. Create a new WinForms application and add a Data Grid (or any other DevExpress data-aware control) onto the form.
  2. Make sure the target .NET framework of your project is of version 4.5 or higher. To check and modify the target framework version, go to "Project | <Your_Project_Name> Properties", then use the "Target framework" editor to select a required version.

    OData - Framework

  3. Install the OData Client for .NET NuGet package. To do so, invoke the Package Manager Console as shown below.

    EFCore - PMC

    Run the following command to install the package.

    PM > 

    Install-Package Microsoft.OData.Client -Version 6.15.0

    OData v4 and OData v2 use different versions of this package, so you may need to upgrade or downgrade it. To do so, go to "Project | Manage NuGet Packages" and locate the installed package. Select it, then change its version in the package details page, as shown below.

    OData V2 - Downgrade Package

After completing these steps you can proceed to binding your data-aware control to OData v4 or OData v2.

Binding To OData V4 Services Using the Wizard

This section is dedicated to binding DevExpress data-aware controls by utilizing the Data Source Configuration Wizard.

  1. Right click your project in Visual Studio and select "Add | New Item...", then switch to the "Online" tab. Find and install the "OData v4 Client Code Generator" item template.

    OData - Install Code Generator

    NOTE

    This step is required for your first OData v4 application only. Once this VS template has been installed, it will be available for all future projects.

  2. Invoke the wizard and select "OData V4 Services". Click the "New Data Source..." button and select the "OData Client" option, then click "Add" to proceed.

    OData - New Data Source

  3. After the new data source is ready, you will be taken to the .tt file with settings. In this file, locate the MetadataDocumentUri property and set it to a valid service document URI or a local file path. For testing purposes you can utilize any online sample service, e.g. "http://services.odata.org/V4/OData/OData.svc/" or TripPin Service.

    
    public static class Configuration
    {
        public const string MetadataDocumentUri = "http://services.odata.org/V4/OData/OData.svc/";
        // . . .
    }
    
  4. Rebuild the project, then invoke the Data Source Configuration Wizard again. This time, you will see your added Data Source in the list. Select it and click "Next".

    OData - Select Data Source

  5. On the next Wizard page, you can select how exactly your data should be connected.

    OData - Select Binding Mode

    • Direct Binding to Data Source - the data-aware control will bind directly to the data source, without any go-between components involved.
    • Binding via the BindingSource Component - a new System.Windows.Forms.BindingSource component will be spawned to transfer data to your data-aware control.
    • Asynchronous Server-Side Data Processing - data will be transfered by the ODataInstantFeedbackSource component.
    • Server-Side Data Processing - data will be transfered by the ODataServerModeSource component.

    Select a required connection mode and click "Next".

  6. The final Wizard page requires you to enter the data service root URL and a table from which to display data. For server and instant feedback modes, you can also choose a data field that serves as a key expression, and sort all data by another data field.

    OData - Last Wizard Page

    When you enter the URL, click the "Test Connection" button. If its initial red glow is gone and the button becomes disabled, the connection was established successfully.

    OData - Test Connection

  7. After you have filled all required fields, press "Finish". Your data-aware control is now bound to Open Data.

Binding To OData V2 Services Using the Wizard

  1. Invoke the wizard and select "WCF Data Services". Click the "New Data Source..." button to create a new source or select an existing one from the list.

    OData v2 - New Source

  2. In the "Add Service Reference" dialog that appears on screen, enter the OData service URL and click "Go" to establish a connection (try "http://services.odata.org/V2/Northwind/Northwind.svc/" for testing purposes). In case of a successful connection, you will see all available services in the "Services" list. Expand a service to browse its data tables, then select the desired service and click "OK".

    OData V2 - Select Service

  3. Rebuild the project, then invoke the Data Source Configuration Wizard again. You will now see your service in the "Data Sources" list. Select it and click "Next".

    OData V2 - Select Data Source

  4. On the next Wizard page, you can select exactly how your data should be connected.

    OData - Select Binding Mode

    • Direct Binding to Data Source - the data-aware control will bind directly to the data source, without any go-between components involved.
    • Binding via the BindingSource Component - a new System.Windows.Forms.BindingSource component will be spawned to transfer data to your data-aware control.
    • Asynchronous Server-Side Data Processing - data will be transfered by the WcfInstantFeedbackSource component.
    • Server-Side Data Processing - data will be transfered by the WcfServerModeSource component.

    Select a required connection mode and click "Next".

  5. The final Wizard page requires you to enter the data service root URL and a table from which to display data. For server and instant feedback modes, you can also choose a data field that serves as a key expression, and sort all data by another data field.

    OData V2 - Final Wizard Page

    When you enter the URL, click the "Test Connection" button. If its initial red glow is gone and the button becomes disabled, the connection was established successfully.

  6. After you have filled all required fields, press "Finish". Your data-aware control is now bound to Open Data.

Binding To OData V4 Services In Code

The code below illustrates how to bind a Data Grid control to open data directly in code. The grid will bind to an existing "DemoService" service, generated by the OData v4 Client Code Generator. To learn how to bind to open data in Server or Instant Feedback modes, refer to the ODataInstantFeedbackSource and ODataServerModeSource component descriptions.


public MainForm() {
    InitializeComponent();
    gridControl1.DataSource = new ODataDemoProject.DemoService(new System.Uri("http://services.odata.org/V4/OData/OData.svc/")).Categories.ToList();
}

Binding To OData V2 Services In Code

The code below illustrates how to bind to an existing "ServiceReference3" reference directly. To learn about binding in Server or Instant Feedback modes, refer to the WcfInstantFeedbackSource and WcfServerModeSource component descriptions.


public MainForm() {
    InitializeComponent();
    gridControl1.DataSource = new MyApp.ServiceReference3.NorthwindEntities(
        new System.Uri("http://services.odata.org/V2/Northwind/Northwind.svc/")).Categories.ToList();
}