This is a migrated thread and some comments may be shown as answers.

Howto export selected rows to CSV

6 Answers 416 Views
GridView
This is a migrated thread and some comments may be shown as answers.
FMorales
Top achievements
Rank 1
FMorales asked on 03 Jun 2016, 08:53 AM

Hi,

does is possible to export to CSV only selected rows of the grid?

How could I perform this action instead export all the content of the grid?

Thanks a lot

Francisco

6 Answers, 1 is accepted

Sort by
1
Andrea
Top achievements
Rank 1
answered on 03 Jun 2016, 02:59 PM

Do not know if it is available out of the box, the following is from my codebase

public static System.IO.TextWriter ExportCSV(System.IO.TextWriter sw, Telerik.WinControls.UI.RadGridView rgv, bool withHeaders)
{    
    string rowSep = "\r\n";
    string listSep = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
    string sep = "";
    string quoteSeq = "\"";
    string quoteSeqEscape = "\"\"";
 
    Action newLine = () =>
    {
        if (sep != "")
            sw.Write(rowSep);
        sep = "";
    };
 
    Action<string> newCell = (x) =>
    {
        sw.Write(sep); //field separator
 
        if (x.Contains(listSep) || x.Any(a => a < 32))
        {
            sw.Write(quoteSeq);
            sw.Write(x.Replace(quoteSeq, quoteSeqEscape));
            sw.Write(quoteSeq);
        }
        else
            sw.Write(x);
 
        sep = listSep;
    };
 
    if (withHeaders)
    {
        foreach (var c in rgv.Columns)
        {
            if (c.IsVisible)
            {
                newCell(c.HeaderText);
            }
        }
    }
 
    foreach (var row in rgv.SelectedRows)
    {
        newLine();
        foreach (var c in rgv.Columns)
        {
            if (c.IsVisible)
            {
                newCell(row.Cells[c.Index].Value?.ToString() ?? "");
            }
        }
    }           
    return sw;
}
0
FMorales
Top achievements
Rank 1
answered on 06 Jun 2016, 07:34 AM

Hi, 

what I mean is to use the native exportation of the grid described here to perform the action:

http://docs.telerik.com/devtools/winforms/gridview/exporting-data/export-to-csv

maybe this methods has a filter or a way to export only selected  rows instead all table.

Thanks

Francisco

0
Dimitar
Telerik team
answered on 07 Jun 2016, 10:10 AM
Hello Francisco,

Thank you for writing.

We do not have any build in functionality for exporting the selected rows only. In this case, you can continue to use your solution. Another approach would be to use the SpreadProecessing library. We have an example which shows how you can create similar custom exporter in our demo application (see attached image).

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Telerik
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
FMorales
Top achievements
Rank 1
answered on 07 Jun 2016, 12:56 PM

Hi, 

thank you very much for your help, i will take a look to the "SpreadProecessing library", anyway ... I am thinking in to create a new one hidden grid and after invoke the exportation, add the selected rows to this new grid and make the export from the hidden grid. I think that this could be easy and enought for me.

Francisco

0
Dimitar
Telerik team
answered on 09 Jun 2016, 08:45 AM
Hi Francisco,

Thank you for writing back.

It is pretty simple to manually create the file as well. For example:
private void radButton1_Click(object sender, EventArgs e)
{
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets.Add();
  
 
    int rowIndex = 0;
    foreach (var row in radGridView1.SelectedRows)
    {
        for (int i = 0; i < row.Cells.Count; i++)
        {
            object value = row.Cells[i].Value;
            worksheet.Cells[rowIndex, i].SetValue(value.ToString());
        }
        rowIndex++;
    }
 
    string fileName = @"D:\SampleFile.csv";
    IWorkbookFormatProvider formatProvider = new CsvFormatProvider();
    using (FileStream output = new FileStream(fileName, FileMode.Create))
    {
        formatProvider.Export(workbook, output);
    }
 
}

Please let me know if there is something else I can help you with. 
 
Regards,
Dimitar
Telerik
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
1
Entropy69
Top achievements
Rank 1
Veteran
answered on 04 Dec 2020, 10:10 AM

Old topic but to whom it may apply:

Just before export I hide all the non selected rows, export with 

exporter.HiddenRowOption = Export.HiddenOption.DoNotExport

after that I make them visible again.

works like a charm.
Tags
GridView
Asked by
FMorales
Top achievements
Rank 1
Answers by
Andrea
Top achievements
Rank 1
FMorales
Top achievements
Rank 1
Dimitar
Telerik team
Entropy69
Top achievements
Rank 1
Veteran
Share this question
or