Xceed DataGrid for Silverlight Documentation
CsvExporter Class
Members  Example 


Class that allows data items to be exported using the comma-separated format (CSV) and the tab-separated formats.

Object Model
CsvExporter ClassCsvFormatSettings Class
Syntax
'Declaration
 
Public Class CsvExporter 
   Inherits TextExporter
'Usage
 
Dim instance As CsvExporter
public class CsvExporter : TextExporter 
Remarks

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.

When exporting data items to the clipboard for the first item, a prompt will be displayed by Silverlight asking if access to the clipboard is to be granted for the application.

Example
This example demonstrates how to synchronously export to the clipboard using CSV format.
<sldg:DataGridControl x:Name="sldgDataGridControl"   
                      ItemsSource="{Binding Path=People}">               
    <sldg:DataGridControl.FixedFooters>
        <Button Content="Copy To Clipboard"
                Click="CsvExport_Click"/>
    </sldg:DataGridControl.FixedFooters>
</sldg:DataGridControl>
Private Sub CsvExport_Click( ByVal sender As Object, ByVal e As RoutedEventArgs )
   If Me.sldgDataGridControl.SelectedRanges.Count > 0 Then
      Me.ExportToCsv( Me.sldgDataGridControl.SelectedRanges )
   End If
End Sub 

Private Sub ExportToCsv( ByVal selectionRanges  As IList( Of SelectionRange ) )
   Dim csvExporter As New CsvExporter( Me.sldgDataGridControl )
   csvExporter.IncludeColumnHeaders = False 

   'Set properties that are null by default.
   csvExporter.FormatSettings.Culture = CultureInfo.CurrentCulture
   csvExporter.FormatSettings.Separator = ';' 

   Dim stream AS new MemoryStream()
   ' Call the synchronous method, so the items can be pasted to the clipboard within a user-initiated event.
   csvExporter.Export( stream, selectionRanges ) 

   Me.CopyItemsToClipboard( stream )
   stream.Close()
   stream.Dispose()  
End Sub 

Private Sub CopyItemsToClipboard( ByVal stream As Stream )
   Dim copyboardText As String = String.Empty
   Dim encoding As Encoding = Encoding.Unicode
   Dim buffer As New Byte( 32768 )
   Dim bytesRead As Integer = 0

   stream.Position = 0 

   ' Get the content of the exported stream as a string.
   While( ( bytesRead = stream.Read( buffer, 0, buffer.Length ) ) > 0 )   
      copyboardText += encoding.GetString( buffer, 0, bytesRead )
   End While 

   ' Copy it to the clipboard.
   Clipboard.SetText( copyboardText )
End Sub
private void CsvExport_Click( object sender, RoutedEventArgs e )
{
   if( this.sldgDataGridControl.SelectedRanges.Count > 0 )
      this.ExportToCsv( this.sldgDataGridControl.SelectedRanges );
} 

private void ExportToCsv( IList<SelectionRange> selectionRanges )
{
   CsvExporter csvExporter = new CsvExporter( this.sldgDataGridControl );
   csvExporter.IncludeColumnHeaders = false; 

   //Set properties that are null by default.
   csvExporter.FormatSettings.Culture = CultureInfo.CurrentCulture;
   csvExporter.FormatSettings.Separator = ';'; 

   using( Stream stream = new MemoryStream() )
   {
      // Call the synchronous method, so the items can be pasted to the clipboard within a user-initiated event.
      csvExporter.Export( stream, selectionRanges ); 

      this.CopyItemsToClipboard( stream );
      stream.Close();
   }
} 

private void CopyItemsToClipboard( Stream stream )
{
   string copyboardText = string.Empty;
   Encoding encoding = Encoding.Unicode;
   byte[] buffer = new byte[ 32768 ];
   int bytesRead = 0;
   stream.Position = 0; 

   // Get the content of the exported stream as a string.
   while( ( bytesRead = stream.Read( buffer, 0, buffer.Length ) ) > 0 )
   {
      copyboardText += encoding.GetString( buffer, 0, bytesRead );
   } 

   // Copy it to the clipboard.
   Clipboard.SetText( copyboardText );
}
Inheritance Hierarchy

System.Object
   Xceed.Silverlight.DataGrid.Export.ExporterBase
      Xceed.Silverlight.DataGrid.Export.TextExporter
         Xceed.Silverlight.DataGrid.Export.CsvExporter

Requirements

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

See Also

Reference

CsvExporter Members
Xceed.Silverlight.DataGrid.Export Namespace

Manipulating Data

Exporting Data

Send Feedback