Declined
Last Updated: 16 Feb 2024 22:24 by ADMIN
Czeshirecat
Created on: 15 Sep 2022 11:33
Category: GridView
Type: Bug Report
0
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;
        columnIndex++;
        sb.AppendLine("");
      }
      else if (!string.IsNullOrEmpty(sb.ToString()))
        sb.Append(",");

      var value = cell.Value;
      worksheet.Cells[columnIndex, rowIndex++ ].SetValue(value.ToString());
      sb.Append($"\"{value.ToString()}\"");
    }

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

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

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

  }

2 comments
ADMIN
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.

Regards,
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.

ADMIN
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()
        {
            InitializeComponent();

            this.radGridView1.Columns.Add("Id");
            this.radGridView1.Columns.Add("Title");
            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)
            {
                dummyGrid.Columns.Add(col.Name); 
            }
            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;
                }
                dummyGrid.Rows.Add(row);
            }
            
            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:
https://www.telerik.com/forums/howto-export-selected-rows-to-csv#3999796 

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.

Regards,
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.