Xceed DataGrid for Silverlight Documentation
BeginExport Method
Example 


The Stream to which the data items will be exported.
An IList of the SelectionRange objects representing the data items to be exported. If a null reference (Nothing in Visual Basic), all items will be exported.
The AsyncCallback delegate to be called when the operation is complete.
An object containing state information for the operation.
Begins the asynchronous export operation specifying the stream to which to export the data items and the selection range representing the items to export.
Syntax
'Declaration
 
Public Function BeginExport( _
   ByVal stream As Stream, _
   ByVal selectionRanges As IList(Of SelectionRange), _
   ByVal asyncCallback As AsyncCallback, _
   ByVal asyncState As Object _
) As IAsyncResult
'Usage
 
Dim instance As ExporterBase
Dim stream As Stream
Dim selectionRanges As IList(Of SelectionRange)
Dim asyncCallback As AsyncCallback
Dim asyncState As Object
Dim value As IAsyncResult
 
value = instance.BeginExport(stream, selectionRanges, asyncCallback, asyncState)

Parameters

stream
The Stream to which the data items will be exported.
selectionRanges
An IList of the SelectionRange objects representing the data items to be exported. If a null reference (Nothing in Visual Basic), all items will be exported.
asyncCallback
The AsyncCallback delegate to be called when the operation is complete.
asyncState
An object containing state information for the operation.

Return Value

An IAsyncResult representing the result of the operation.
Remarks

The EndExport must always be called in order to end the export operation. For synchronous export operations, the Export method can be used.

Example

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 );
   }
}
Requirements

Target Platforms: Windows 7, Windows Vista, Windows XP SP3, Windows Server 2008 family

See Also

Reference

ExporterBase Class
ExporterBase Members
EndExport Method

Manipulating Data

Exporting Data

Send Feedback