Intersoft ClientUI Documentation
Exporting Data with UXGridView

This topic provides an overview of the data exporting feature in UXGridView, discusses the supported exporting format, and explains how to export paged data using MVVM pattern. For information about UXGridView and its features in general, see UXGridView Overview.

Enable Data Exporting

To enable data exporting in UXGridView, you simply set the CanUserExport property of UXGridView to true

XAML
Copy Code
<Intersoft:UXGridView CanUserExport="True"/>

When enabled, an Export dropdown button will appear in the status bar element of UXGridView.

Supported Format

You can select the format of the exported results from the context menu as shown in the figure above. The supported export format are:

Furthermore, you can also customize which element to export through the provided ExportOptions property. The following code shows how to include column footers and group footers in the data export results.

IncludeColumnFooters and IncludeGroupFooters are only applicable for HTML format.
XAML
Copy Code
<Intersoft:UXGridView CanUserExport="{Binding CanUserExport}">
    <Intersoft:UXGridView.ExportOptions>
        <Intersoft:UXGridViewExportOptions IncludeColumnFooters="True" IncludeColumnHeaders="True" IncludeGroupFooters="True" />                       
    </Intersoft:UXGridView.ExportOptions>                
</Intersoft:UXGridView>

Exporting Paged Data 

By default, the exporting processes only the data available in the client. Therefore if you enable server paging, you might not be able to export the complete data correctly. To address this challenge, you can implement the ExportCommand in the ViewModel and provide the desired data to the ExportItems property.

The following code shows you how to bind ExportItems and ExportCommand in the ViewModel and supply the data to export.

View Model
Copy Code
using System.Collections;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using HowToSamples.Web;
using Intersoft.Client.Data.ComponentModel;
using Intersoft.Client.Data.Provider.Ria;
using Intersoft.Client.Framework.Input;

namespace HowToSamples.ViewModels
{
    public class ExportingViewModel: ViewModelBase
    {
        public ExportingViewModel()
        {
            this.Manager = new NorthwindDomainContext();
            this.CanUserExport = true;
            this.QueryDescriptor = new QueryDescriptor();
            this.ExportCommand = new DelegateCommand(ExecuteExportCommand);
        }

        private bool _canUserExport;
        private IEnumerable _exportItems;
        private IEnumerable _products;
        private QueryDescriptor _queryDescriptor;
       
        private NorthwindDomainContext Manager { get; set; }

        public bool CanUserExport
        {
            get { return this._canUserExport; }
            set
            {
                if (this._canUserExport != value)
                {
                    this._canUserExport = value;
                    this.OnPropertyChanged("CanUserExport");
                }
            }
        }

        public DelegateCommand ExportCommand { get; set; }

        public IEnumerable ExportItems
        {
            get { return this._exportItems; }
            set
            {
                if (this._exportItems != value)
                {
                    this._exportItems = value;
                    this.OnPropertyChanged("ExportItems");
                }
            }
        }

        public IEnumerable Products
        {
            get { return this._products; }
            set
            {
                if (this._products != value)
                {
                    this._products = value;
                    this.OnPropertyChanged("Products");
                }
            }
        }

        public QueryDescriptor QueryDescriptor
        {
            get
            {
                return this._queryDescriptor;
            }
            set
            {
                if (this._queryDescriptor != value)
                {
                    if (this._queryDescriptor != null)
                        this._queryDescriptor.QueryChanged -= new System.EventHandler(OnQueryChanged);

                    this._queryDescriptor = value;
                    this._queryDescriptor.QueryChanged += new System.EventHandler(OnQueryChanged);

                    this.OnPropertyChanged("QueryDescriptor");
                }
            }
        }

        public void ExecuteExportCommand(object parameter)
        {
            Intersoft.Client.Data.ComponentModel.QueryDescriptor queryDescriptor = this.QueryDescriptor.CreateCopy(true, true, false); // copy the query descriptor, but exclude the page descriptor
            var query = this.Manager.GetProductsQuery().OrderBy(p => p.ProductID).Parse(queryDescriptor);

            this.Manager.Load(
               query,
               op =>
               {
                   if (op.IsComplete)
                   {
                       Intersoft.Client.Data.ComponentModel.PagedCollectionView current = this.Products as Intersoft.Client.Data.ComponentModel.PagedCollectionView;
                       Intersoft.Client.Data.ComponentModel.PagedCollectionView exportedItems = new Intersoft.Client.Data.ComponentModel.PagedCollectionView(op.Entities);

                       exportedItems.CopyDefinitionsFrom(current, true, false, false, false);
                       this.ExportItems = exportedItems; // supply the export process with correct collection.
                   }
                   else
                   {
                       MessageBox.Show(op.Error.ToString());
                   }
               },

               true);            
        }

        public virtual void LoadProducts()
        {
            if (Intersoft.Client.Framework.ISControl.IsInDesignModeStatic)
                return;

            var query = this.Manager.GetProductsQuery().OrderBy(p => p.ProductID).Parse(this.QueryDescriptor);
            query.IncludeTotalCount = true;

            this.Manager.Load(
               query,
               op =>
               {
                   if (op.IsComplete)
                   {
                       this.Products = new Intersoft.Client.Data.ComponentModel.PagedCollectionView(op.Entities);
                       this.QueryDescriptor.PageDescriptor.TotalItemCount = op.TotalEntityCount;
                   }
                   else
                   {
                       MessageBox.Show(op.Error.ToString());
                   }
               },

               true);
        }

        private void OnQueryChanged(object sender, System.EventArgs e)
        {            
            this.LoadProducts();
        }
    }
}
View
Copy Code
    


<UserControl x:Class="HowToSamples.Exporting"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Intersoft="http://intersoft.clientui.com/schemas"
    xmlns:ViewModels="clr-namespace:HowToSamples.ViewModels"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.DataContext>
            <ViewModels:ExportingViewModel/>
        </Grid.DataContext>
        <Intersoft:UXGridView AutoGenerateColumns="False" QueryOperation="Server"
                              CanUserPage="True" PageSize="20"                              
                              ItemsSource="{Binding Products}" 
                              SortDescriptors="{Binding QueryDescriptor.SortDescriptors, Mode=TwoWay}"
                              PageDescriptor="{Binding QueryDescriptor.PageDescriptor}"                              
                              CanUserExport="{Binding CanUserExport}"                              
                              ExportCommand="{Binding ExportCommand}"
                              ExportItems="{Binding ExportItems}">

        <Intersoft:UXGridView.GroupDescriptors>
            <Intersoft:UXGridViewGroupDescriptor PropertyName="CategoryID"/>
        </Intersoft:UXGridView.GroupDescriptors>

        <Intersoft:UXGridView.Columns>
            <Intersoft:UXGridViewTextColumn Header="Category ID" Binding="{Binding CategoryID}"/>
            <Intersoft:UXGridViewTextColumn Header="Product ID" Binding="{Binding ProductID}" Aggregate="Count" FooterFormatString="Count = {0}"/>
            <Intersoft:UXGridViewTextColumn Header="Product Name" Binding="{Binding ProductName}"/>
            <Intersoft:UXGridViewTextColumn Header="Units In Stock" Binding="{Binding UnitsInStock}" Aggregate="Max" FooterFormatString="Max = {0}"/>
            <Intersoft:UXGridViewTextColumn Header="Unit Price" Binding="{Binding UnitPrice}" Aggregate="Avg" FooterFormatString="Avg = {0:n2}"/>
            <Intersoft:UXGridViewTextColumn Header="Units On Order" Binding="{Binding UnitsOnOrder}" Aggregate="Min" FooterFormatString="Min = {0}"/>
            <Intersoft:UXGridViewTextColumn Header="Quantity Per Unit" Binding="{Binding QuantityPerUnit}"/>
        </Intersoft:UXGridView.Columns>

        </Intersoft:UXGridView>
    </Grid>
</UserControl>

 

See Also