$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
DataGridView WinForms Control
Export grid data to excel, CSV, HTML, and PDF files in C# WinForms Applications.
Home > WinForms UI Controls > User Manual > Export Grid Data to CSV in C#

Export Data to CSV from GridView

The Kettic GridView .NET component provides the support of exporting data from grid to CSV file. Moreover, it allows the users to customize the exported data by using the formatting events. To export the data to the CSV, we need to use the KetticData.dll assembly and include the C# namespaces to access to the types contained in KetticData, like Kettic.WinForms.Data and Kettic.WinForms.UI.Export.

How to Export Data from Grid to CSV in C#

To export the data from grid to CSV, we need to initialize the ExportToCSV C# class first. And the constructor will take the parameter, KetticGridView, which will be exported. The simple C# code below shows that how to initialize the ExportToCSV object.

ExportToCSV exporter = new ExportToCSV(this.ketticGridView1);
After initializing the ExportToCSV C# class, we can use the FileExtension property to change the file extension, *.csv, for the exported file.

exporter.FileExtension = "";
While we are going to export data in grid, the hidden columns and rows can be exported or not. This is accomplished through the properties, HiddenColumnOption and HiddenRowOption. There are two valid options available to the users, ExportAlways and DoNotExport. The ExportAsHidden option are not supported by CSV format.
The Kettic GridView .NET control offers the SummariesExportOption property to specify how to export summary items. There are four options available for selecting: ExportAll, ExportOnlyTop, ExportOnlyBottom, and DoNotExport. The default setting is ExportAll. The C# code below shows how to set the SummariesExportOption property.

exporter.SummariesExportOption = SummariesOption.DoNotExport;
After setting the above properties, we can export the data from grid to CSV file now. To export data from grid to CSV, we need to use the ApplyExport method included in the ExportToCSV object. Its valid parameter includes fileName which define the name of the exported csv file.

string fileName = "C:\\ExportedGridData1.csv";
The Kettic GridView offers the CSVFormatCell event for the users to create formatting grid cell that will be exported. It is able to access to a single cell element to replace the actual value for all cells that is relevant to the exported KetticGridView. The following C# code shows how to handle the CSVFormatCell event.

void exporter_CSVFormatCell(object sender, Kettic.WinForms.UI.Export.CSV.CSVFormatCellEventArgs e)
if (e.GridColumnIndex == 1 && e.GridRowInformationType == typeof(GridViewDataRowInformation))
e.CSVCellElement.Value = "Value for Test";
The Kettic GridView offers the CSVTableCreated event for the users to use together with the public ApplyCustomCSVRow method. This allows the users to create and format new custom grid rows on the top of the csv file. The following C# code shows how to handle the CSVTableCreated event.

void exporter_CSVTableCreated(object sender, Kettic.WinForms.UI.Export.CSV.CSVTableCreatedEventArgs e)
((ExportToCSV)sender).ApplyCustomCSVRow(e.CSVTableElement, "Caption");
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls