Last Updated: 16 Feb 2024 22:24 by ADMIN
Created on: 15 Sep 2022 11:33
Category: GridView
Type: Bug Report
CsvFormatProvider. (Winforms, C#) 'Settings' property is private so one can't set up a csv export correctly

C#. net core 6, winforms application, TekerikNuGet3 package source, UI.for.WinForms.AllControls.NetCore package 2022.2.622

I am trying to export selected cells from a RadGridView as a csv file.

When using "var exporter = new ExportToCSV(Dgv)" then "exporter.RunExport(fileName);" writes the csv file correctly. It exports every cell.

It doesn't support exporting selections so I wrote a function to do this . The one below is what I used in my project to test what I was doing. It initially writes the csv to a worksheet and wrote that using formatprovider to file, then I just created a stringbuilder adding quotes and appended that to the file afterwards.

Writing the worksheet, the csv values haven't been quoted, plus number fields have had leading zeros removed which proves to be a problem when telephone numbers are stored in a field.

So I googled and found the Settings property which is there to set csv options. But they are private, not public therefore I can't set up the csv propertly.

private void radButton1_Click(object sender, EventArgs e)

    var workbook = new Workbook();
    var worksheet = workbook.Worksheets.Add();

    var rowIndex = 0;
    var columnIndex = -1;
    var sb = new StringBuilder();
    var prevColumnIndex = -1;

    // ForEach cell are accessed vertically then horizontally.
    foreach (var cell in Dgv.SelectedCells)
      // Cell is included in selection even if it's invisible
      // so check visibility and ignore if it isnt
      if (!cell.ColumnInfo.IsVisible) continue;

      // At bottom of column, rownum will change. Watch for this
      // and reset x and y values
      var rowNum = cell.RowInfo.Index;
      if (rowNum != prevColumnIndex)
        prevColumnIndex = rowNum;
        rowIndex = 0;
      else if (!string.IsNullOrEmpty(sb.ToString()))

      var value = cell.Value;
      worksheet.Cells[columnIndex, rowIndex++ ].SetValue(value.ToString());

    var fileName = @"C:\temp\SampleFile.csv";

    IWorkbookFormatProvider formatProvider = new CsvFormatProvider();
    using var output = new FileStream(fileName, FileMode.Create);
    formatProvider.Export(workbook, output);

    // Write contents of sb to the file for comparison sake
    File.AppendAllText(fileName, sb.ToString());


Dinko | Tech Support Engineer
Posted on: 16 Feb 2024 22:24

Hi Claire,

The feedback item status is changed to Declined as exporting the selected cells/rows is not supported out of the box.

Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Dess | Tech Support Engineer, Principal
Posted on: 22 Sep 2022 06:06

Hello, Claire, 

Indeed, the export functionality doesn't allow exporting only the selected rows. However, I would recommend you an easier option to use a dummy RadGridView populated just with the selected rows and then export this grid using the default functionality. 

        public RadForm1()

            for (int i = 0; i < 10; i++)
                this.radGridView1.Rows.Add(i, Guid.NewGuid().ToString());
            this.radGridView1.MultiSelect = true;
            this.radGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect;

        private void radButton1_Click(object sender, EventArgs e)
            RadGridView dummyGrid = new RadGridView();
            foreach (GridViewColumn col in this.radGridView1.Columns)
            foreach (GridViewRowInfo selectedRow in this.radGridView1.SelectedRows)
                GridViewRowInfo row = dummyGrid.Rows.NewRow();
                foreach (GridViewCellInfo cell in selectedRow.Cells)
                    row.Cells[cell.ColumnInfo.Name].Value = cell.Value;
            GridViewSpreadExport spreadExporter = new GridViewSpreadExport(dummyGrid);
            spreadExporter.ExportFormat = SpreadExportFormat.Csv;
            SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
            spreadExporter.RunExport(@"..\..\" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".csv", exportRenderer); 

Alternatively, you can create a Workbook, feel the cells with the desired data and then export it. A sample approach is demonstrated here: 

Feel free to use this approach which suits your requirements best.

Off topic, feel free to submit a support ticket if you have any further technical inquiries instead of submitting byg reports. Thus, our support engineers would gladly assist you. Thank you for your understanding.

Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.