Howto export selected rows to CSV

6 posts, 0 answers
  1. FMorales
    FMorales avatar
    26 posts
    Member since:
    Aug 2014

    Posted 03 Jun Link to this post

    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

  2. Andrea
    Andrea avatar
    52 posts
    Member since:
    Oct 2012

    Posted 03 Jun in reply to FMorales Link to this post

    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;
    }
  3. UI for WinForms is Visual Studio 2017 Ready
  4. FMorales
    FMorales avatar
    26 posts
    Member since:
    Aug 2014

    Posted 06 Jun Link to this post

    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

  5. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 07 Jun Link to this post

    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.
  6. FMorales
    FMorales avatar
    26 posts
    Member since:
    Aug 2014

    Posted 07 Jun in reply to Dimitar Link to this post

    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

  7. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 09 Jun Link to this post

    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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready