Skip to main content
All docs
V24.2

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Bind to Excel Data Sources

  • 2 minutes to read

The GridControl can use Microsoft Excel files (XLS, XLSX, or XLSM) or CSV files as data sources.

The following demo uses the ExcelItemSource class to bind the GridControl to data stored in an XLS file:

Run Demo: Excel Items Source

#Prerequisites

The GridControl uses the ExcelItemsSource class to obtain data from Microsoft Excel files. To access ExcelItemsSource, reference the DevExpress.Wpf.Core.Extensions NuGet package or the DevExpress.Xpf.Core.v24.2.Extensions assembly in your project:

<ItemGroup>
    <PackageReference Include="DevExpress.Wpf.Core" Version="24.2.*" />
    <PackageReference Include="DevExpress.Wpf.Core.Extensions" Version="24.2.*" />
    <PackageReference Include="DevExpress.Wpf.Grid" Version="24.2.*" />
    <!-- ... -->
</ItemGroup>

#Define the Data Source

In this example, the data source is the Orders.xls file located in the application root folder:

WPF GridControl Bind to Excel Data - File Location

To use the Orders.xls file as a data source, set the file’s Build Action to Resource:

WPF GridControl Bind to Excel Data - Enable Build Action

To access data stored in the Orders.xls file, define ExcelItemsSource in the resources and specify FileUri, StreamDocumentFormat, and WorksheetName properties as follows:

<dx:ThemedWindow x:Class="GridControlExcel.MainWindow"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
                 xmlns:local="clr-namespace:GridControlExcel" Height="450" Width="800">
    <dx:ThemedWindow.Resources>
        <dx:ExcelItemsSource x:Key="ExcelItemsSource"
                             FileUri="pack://application:,,,/Orders.xls"
                             StreamDocumentFormat="Xls"
                             WorksheetName="Sheet">
        </dx:ExcelItemsSource>
    </dx:ThemedWindow.Resources>

    <Grid>
        <!-- ... -->
    </Grid>
</dx:ThemedWindow>

#Specify Data Source Columns

If you want to display individual columns and specify the column type and order, you can add these columns to the ExcelItemsSource.Columns collection:

<dx:ThemedWindow x:Class="GridControlExcel.MainWindow"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
                 xmlns:sys="clr-namespace:System;assembly=mscorlib"
                 xmlns:local="clr-namespace:GridControlExcel" Height="450" Width="800">
    <dx:ThemedWindow.Resources>
        <dx:ExcelItemsSource x:Key="ExcelItemsSource"
                             FileUri="pack://application:,,,/Orders.xls"
                             StreamDocumentFormat="Xls"
                             WorksheetName="Sheet">
            <dx:ExcelItemsSource.Columns>
                <dx:ExcelColumn Name="ID" ColumnType="{x:Type sys:Double}" IsSelected="False" />
                <dx:ExcelColumn Name="Product Name" ColumnType="{x:Type sys:String}" />
                <dx:ExcelColumn Name="Customer Name" ColumnType="{x:Type sys:String}" />
                <dx:ExcelColumn Name="Country" ColumnType="{x:Type sys:String}" />
                <dx:ExcelColumn Name="City" ColumnType="{x:Type sys:String}" />
                <dx:ExcelColumn Name="Order Date" ColumnType="{x:Type sys:DateTime}" />
                <dx:ExcelColumn Name="Unit Price" ColumnType="{x:Type sys:Double}" />
                <dx:ExcelColumn Name="Quantity" ColumnType="{x:Type sys:Double}" />
            </dx:ExcelItemsSource.Columns>
        </dx:ExcelItemsSource>
    </dx:ThemedWindow.Resources>

    <Grid>
        <!-- ... -->
    </Grid>
</dx:ThemedWindow>

#Bind GridControl to Data

Use the GridControl.ItemsSource property to bind the GridControl to the defined ExcelItemsSource as follows:

<dx:ThemedWindow x:Class="GridControlExcel.MainWindow"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
                 xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
                 xmlns:sys="clr-namespace:System;assembly=mscorlib"
                 xmlns:local="clr-namespace:GridControlExcel" Height="450" Width="800">
    <dx:ThemedWindow.Resources>
        <dx:ExcelItemsSource x:Key="ExcelItemsSource"
                             FileUri="pack://application:,,,/Orders.xls"
                             StreamDocumentFormat="Xls"
                             WorksheetName="Sheet">
            <dx:ExcelItemsSource.Columns>
                <dx:ExcelColumn Name="ID" ColumnType="{x:Type sys:Double}" IsSelected="False" />
                <dx:ExcelColumn Name="Product Name" ColumnType="{x:Type sys:String}" />
                <dx:ExcelColumn Name="Customer Name" ColumnType="{x:Type sys:String}" />
                <dx:ExcelColumn Name="Country" ColumnType="{x:Type sys:String}" />
                <dx:ExcelColumn Name="City" ColumnType="{x:Type sys:String}" />
                <dx:ExcelColumn Name="Order Date" ColumnType="{x:Type sys:DateTime}" />
                <dx:ExcelColumn Name="Unit Price" ColumnType="{x:Type sys:Double}" />
                <dx:ExcelColumn Name="Quantity" ColumnType="{x:Type sys:Double}" />
            </dx:ExcelItemsSource.Columns>
        </dx:ExcelItemsSource>
    </dx:ThemedWindow.Resources>

    <Grid>
        <dxg:GridControl ItemsSource="{Binding Data, Source={StaticResource ExcelItemsSource}}"
                         AutoGenerateColumns="AddNew" />
    </Grid>
</dx:ThemedWindow>