Skip to main content
All docs
V25.1
  • 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.v25.1.Extensions assembly in your project:

    <ItemGroup>
        <PackageReference Include="DevExpress.Wpf.Core" Version="25.1.*" />
        <PackageReference Include="DevExpress.Wpf.Core.Extensions" Version="25.1.*" />
        <PackageReference Include="DevExpress.Wpf.Grid" Version="25.1.*" />
        <!-- ... -->
    </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>