New to Telerik UI for WinForms? Download free 30-day trial

Export to CSV

RELATED VIDEOS
Exporting to CSV with RadGridView for WinForms
In this video, you will learn how to export RadGridView for WinForms to the CSV file format.
WinForms RadGridView Export to CSV Tutorial

Overview

This method offers excellent export performance. It creates a csv file and supports formatting events to allow customizing exported data.

The CSV export functionality is located in the TelerikData.dll assembly. You need to include the following namespaces in order to access the types contained in TelerikData:

  • Telerik.WinControls.Data
  • Telerik.WinControls.UI.Export>

Exporting Data

Initialize ExportToCSV object

Before running export to CSV, you have to initialize the ExportToCSV class. The constructor takes one parameter: the RadGridView that will be exported:

ExportToCSV initialization

ExportToCSV exporter = new ExportToCSV(this.radGridView1);

Dim exporter As ExportToCSV = New ExportToCSV(Me.RadGridView1)

File Extension

This property allows for changing the default (*.csv) file extension of the exported result file:

Setting the file extension

exporter.FileExtension = "";

exporter.FileExtension = ""

Hidden Columns and Rows Option

You can choose if the hidden columns and rows should be exported through HiddenColumnOption and HiddenRowOption properties. Please, note that these properties use the standard enums and include the *ExportAsHidden *option, which is not supported by CSV format. Setting that option will not affect the export at all.

  • ExportAlways

  • DoNotExport  (default)

  • ExportAsHidden (not supported in csv)

Summaries export option

You can use SummariesExportOption property to specify how to export summary items. There are four options to choose from:

  • ExportAll (default)

  • ExportOnlyTop

  • ExportOnlyBottom

  • DoNotExport

Setting summaries to export

exporter.SummariesExportOption = SummariesOption.DoNotExport;

exporter.SummariesExportOption = SummariesOption.DoNotExport

RunExport method

Exporting data to CSV file is done through the RunExport method of the ExportToCSV object. The RunExport method accepts the following parameter:

  • fileName: the name of the exported file

Export to CVS format

string fileName = "C:\\ExportedData.csv";
exporter.RunExport(fileName);

Dim fileName As String = "C:\\ExportedData.csv"
exporter.RunExport(fileName)

Events

CSVCellFormating event

It gives access to a single cell’s element that allows you to replace the actual value for every cell related to the exported RadGridView:

Handling the CSVCellFormatting event

void exporter_CSVCellFormatting(object sender, Telerik.WinControls.UI.Export.CSV.CSVCellFormattingEventArgs e)
{
    if (e.GridColumnIndex == 1 && e.GridRowInfoType == typeof(GridViewDataRowInfo))
    {
        e.CSVCellElement.Value =  "test value";
    }
}

Private Sub exporter_CSVCellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Export.CSV.CSVCellFormattingEventArgs)
    If (e.GridColumnIndex = 1 AndAlso e.GridRowInfoType.Equals(GetType(GridViewDataRowInfo))) Then
        e.CSVCellElement.Value = "test value"
    End If
End Sub

CSVTableCreated event:

It can be used together with the public method AddCustomCSVRow. It allows for adding and formatting new custom rows on the top of the csv file :

Handling the CSVTableCreated event

void exporter_CSVTableCreated(object sender, Telerik.WinControls.UI.Export.CSV.CSVTableCreatedEventArgs e)
{
    ((ExportToCSV)sender).AddCustomCSVRow(e.CSVTableElement, "MY TABLE CAPTION");
}

Private Sub exporter_CSVTableCreated(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Export.CSV.CSVTableCreatedEventArgs)
    DirectCast(sender, ExportToCSV).AddCustomCSVRow(e.CSVTableElement, "MY TABLE CAPTION")
End Sub

See Also

In this article