Xceed.Silverlight.DataGrid.v2.0 Assembly > Xceed.Silverlight.DataGrid.Export Namespace : XmlssExporter Class |
'Declaration Public Class XmlssExporter Inherits TextExporter
'Usage Dim instance As XmlssExporter
public class XmlssExporter : TextExporter
Because only one export operation can be executed at a time, it is recommended to disable any buttons or controls that launch export operations until the current operation completes.
The XmlssExporter and CsvExporter classes also expose the Export method, which can be used to synchronously export data items; however, it can only be used with a non-asynchronous data source (e.g., local list) and the UI will be "frozen" until the operation returns. It is highly recommended to only use this method if a small amount of data items are to be exported.
This examples demonstrates how to asynchronously export to XML spreadsheet format.
The code for the InverseBooleanConverter class can be found in the Additional Resources section of the documentation.
<sldg:DataGridControl x:Name="sldgDataGridControl" ItemsSource="{Binding Path=Orders}"> <sldg:DataGridControl.Resources> <local:InverseBooleanConverter x:Key="inverseBooleanConverter"/> </sldg:DataGridControl.Resources> <sldg:DataGridControl.GroupDescriptions> <sldg:DataGridGroupDescription PropertyName="ShipCountry"/> </sldg:DataGridControl.GroupDescriptions> <sldg:DataGridControl.Columns> <sldg:Column FieldName="ShipCountry" Title="COUNTRY"/> </sldg:DataGridControl.Columns> <sldg:DataGridControl.FixedFooters> <StackPanel> <Button Click="ExportAll_Click" Content="Export All" IsEnabled="{Binding Path=IsExporting, Converter={StaticResource inverseBooleanConverter}}"/> <Button Content="Export Selected" Click="ExportSelected_Click" IsEnabled="{Binding Path=IsExporting, Converter={StaticResource inverseBooleanConverter}}"/> <Button Content="Export Custom" Click="ExportCustomSelection_Click" IsEnabled="{Binding Path=IsExporting, Converter={StaticResource inverseBooleanConverter}}"/> <Button Content="Cancel Export" Click="CancelExport_Click"/> </StackPanel> </sldg:DataGridControl.FixedFooters> </sldg:DataGridControl>
Private Sub ExportAll_Click( ByVal sender As Object, ByVal e As RoutedEventArgs ) Me.ExportData( Nothing ) End Sub Private Sub ExportSelected_Click( ByVal sender As Object, ByVal e As RoutedEventArgs ) Me.ExportData( Me.sldgDataGridControl.SelectedRanges ) End Sub Private Sub ExportCustomSelection_Click( ByVal sender As Object, ByVal e As RoutedEventArgs ) Dim selectionRange As New SelectionRange( NewSortDescription() { New SortDescription( "ShipCity", ListSortDirection.Ascending ) }, Nothing, Nothing ) selectionRange.StartRangeInfos.Add( "ShipCity", "L" ) selectionRange.StartRangeInfos.IsInclusive = True selectionRange.EndRangeInfos.ToEnd() Me.ExportData( New SelectionRange() { selectionRange } ) End Sub Private m_exporter As XmlssExporter Private Sub ExportData( ByVal selectionRanges As IList( Of SelectionRange) ) Me.IsExporting = True Dim saveFileDiag As New SaveFileDialog() saveFileDiag.Filter = "XML Spreadsheet (*.xml)|*.xml|All files (*.*)|*.*" m_exporter = New XmlssExporter( Me.sldgDataGridControl ) m_exporter.IncludeColumnHeaders = True m_exporter.UseFieldNamesInHeaders = False m_exporter.UseFixedHeaderRow = False If( saveFileDiag.ShowDialog().GetValueOrDefault() ) Then Dim stream As Stream = saveFileDiag.OpenFile() Dim asyncResult As IAsyncResult = m_exporter.BeginExport( stream, selectionRanges, New AsyncCallback( AddressOf Me.EndExportToFile ), Nothing ) ' BeginExport() is already completed, call EndExport right away If asyncResult.IsCompleted Then stream = m_exporter.EndExport( asyncResult ) stream.Close() stream.Dispose() End If Else Me.IsExporting = False End If End Sub Private Sub EndExportToFile( ByVal asyncResult As IAsyncResult ) ' BeginExport() was completed synchronously and will be treated in ExportData(). If asyncResult.CompletedSynchronously Then Return End If Dim stream As Stream= m_exporter.EndExport( asyncResult ) stream.Close() stream.Dispose() Me.IsExporting = False End Sub Private Sub CancelExport_Click( ByVal sender As object, ByVal e As RoutedEventArgs ) ' When CancelExport is called, the EndExport callback will follow in which you must call ' the EndExport method and close and dispose of the stream. Calling CancelExport will result ' in a valid, albeit partial, exported document. m_exporter.CancelExport() End Sub ' This dependency property was created so that buttons (or any other control) that launch ' an export operation could bind to it and be disabled during the export operation. ' If an attempt is made to launch an export operation while one is already in progress, ' and exception will be thrown. Public Shared ReadOnly IsExportingProperty As DependencyProperty = DependencyProperty.Register( "IsExporting", GetType( Boolean ), GetType( MainPage ), Nothing ) Property Property IsExporting As Boolean Get Return CBool( GetValue( IsExportingProperty ) ) End Get Set SetValue( IsExportingProperty, value ) End Set End Property
private void ExportAll_Click( object sender, RoutedEventArgs e ) { this.ExportData( null ); } private void ExportSelected_Click( object sender, RoutedEventArgs e ) { this.ExportData( this.sldgDataGridControl.SelectedRanges ); } private void ExportCustomSelection_Click( object sender, RoutedEventArgs e ) { SelectionRange selectionRange = new SelectionRange( new SortDescription[] { new SortDescription( "ShipCity", ListSortDirection.Ascending ) }, null, null ); selectionRange.StartRangeInfos.Add( "ShipCity", "L" ); selectionRange.StartRangeInfos.IsInclusive = true; selectionRange.EndRangeInfos.ToEnd(); this.ExportData( new SelectionRange[] { selectionRange } ); } private XmlssExporter m_exporter; private void ExportData( IList<SelectionRange> selectionRanges ) { this.IsExporting = true; SaveFileDialog saveFileDiag = new SaveFileDialog(); saveFileDiag.Filter = "XML Spreadsheet (*.xml)|*.xml|All files (*.*)|*.*"; m_exporter = new XmlssExporter( this.sldgDataGridControl ); m_exporter.IncludeColumnHeaders = true; m_exporter.UseFieldNamesInHeaders = false; m_exporter.UseFixedHeaderRow = false; if( saveFileDiag.ShowDialog().GetValueOrDefault() ) { Stream stream = saveFileDiag.OpenFile(); IAsyncResult asyncResult = m_exporter.BeginExport( stream, selectionRanges, new AsyncCallback( this.EndExportToFile ), null ); // BeginExport() is already completed, call EndExport right away if( asyncResult.IsCompleted ) { stream = m_exporter.EndExport( asyncResult ); stream.Close(); stream.Dispose(); } } else { this.IsExporting = false; } } private void EndExportToFile( IAsyncResult asyncResult ) { // BeginExport() was completed synchronously and will be treated in ExportData(). if( asyncResult.CompletedSynchronously ) return; Stream stream = m_exporter.EndExport( asyncResult ); stream.Close(); stream.Dispose(); this.IsExporting = false; } private void CancelExport_Click( object sender, RoutedEventArgs e ) { // When CancelExport is called, the EndExport callback will follow in which you must call // the EndExport method and close and dispose of the stream. Calling CancelExport will result // in a valid, albeit partial, exported document. m_exporter.CancelExport(); } // This dependency property was created so that buttons (or any other control) that launch // an export operation could bind to it and be disabled during the export operation. // If an attempt is made to launch an export operation while one is already in progress, // and exception will be thrown. public static readonly DependencyProperty IsExportingProperty = DependencyProperty.Register( "IsExporting", typeof( Boolean ), typeof( MainPage ), null ); public bool IsExporting { get { return ( bool )GetValue( IsExportingProperty ); } set { SetValue( IsExportingProperty, value ); } }
System.Object
Xceed.Silverlight.DataGrid.Export.ExporterBase
Xceed.Silverlight.DataGrid.Export.TextExporter
Xceed.Silverlight.DataGrid.Export.XmlssExporter
Target Platforms: Windows 7, Windows Vista, Windows XP SP3, Windows Server 2008 family