RadGrid Export To CSV

21 posts, 5 answers
  1. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 02 Aug 2011 Link to this post

    Hello!!

    I need to allow exporting all the rows of my RadGrid to .CSV, however after i tested the exportToCSV button, it only exports the results of the first page(showed on the webpage).

    It's possible to export all the GRID data ?? Can you tell me how?

    Another thing I need to know, is how can i export only one column values to a pdf or excell..instead of all the collumns unformatted??
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 02 Aug 2011 Link to this post

    Hello,

    Please use below method if you used "OnNeedDataSource" event for binding datasource
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
            {
                if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName)
                {
          RadGrid1.MasterTableView.Columns.FindByUniqueName("YourCoumnUniqueName").Visible = false; // hide column before export
     
                    RadGrid1.ExportSettings.IgnorePaging = true;      
                }
            }

    Else use below method
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
            {
                if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName)
                {
          RadGrid1.MasterTableView.Columns.FindByUniqueName("YourCoumnUniqueName").Visible = false; // hide column before export
     
                    RadGrid1.ExportSettings.IgnorePaging = true;  
                            RadGrid1.DataSource = ""; // please set data source again.
                            RadGrid1.DataBind();
          
                }
            }



    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 02 Aug 2011 Link to this post

    Hello and thanks, what about export only the first column values to excell or pdf?
  5. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 03 Aug 2011 Link to this post

    Hello,

    int count = 0;
    foreach(GridColumn column in RadGrid1.Columns)          {
    If(count > 0)
         column.Visible= false;
    else if(column.Visible)
       count ++;

     }


    Thanks,
    Jayesh Goyani
  6. Answer
    Rakesh Gupta
    Rakesh Gupta avatar
    12 posts
    Member since:
    Oct 2010

    Posted 03 Aug 2011 Link to this post

    Hello Ricardo,

    You can use the following code to Export only the First Column of the RadGrid.

    The code given by Jayesh will also work but somtimes we are using some id columns which are generally first column and not visible on the Grid at that it will export blank.

    So, i changed the Jayesh's code bit.

    I think this is helpful to you.

    int count = 0;
    foreach (GridColumn column in radGrid.Columns)
    {
         if (column.Visible)
         {
              if (count > 0)
                   column.Visible = false;
              else
                   count++;
         }
    }

    Let me know for any concerns,
    --
    Thanks & Regards,
    Rakesh Gupta
  7. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 05 Aug 2011 Link to this post

    Hello again....i think something is missing...I tried the code bellow and when i click on the button to export csv....it export only the rows that are paged.......
            
     protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
            {
                if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName)
                {
                    RadGrid1.MasterTableView.Columns.FindByUniqueName("SequencialNumber").Visible = false; // hide column before export
      
                    RadGrid1.ExportSettings.IgnorePaging = true;
                    RadGrid1.DataSource = this.SapDocuments; // please set data source again.
                    RadGrid1.DataBind();
      
                }
            }
      
    and aspx:
      
    <div>
            <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowPaging="True"
                AllowSorting="True" AutoGenerateColumns="False" Width="97%" enableajax="True"
                CssClass="productsGrid" CellSpacing="0" DataSourceID="SqlDataSource1" AllowFilteringByColumn="True"
                ShowFooter="True" Skin="Black">
                <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
                <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
                    <Selecting AllowRowSelect="True" />
                </ClientSettings>
                <MasterTableView GridLines="None" Width="100%" CommandItemSettings-ShowExportToCsvButton="True"
                    CommandItemSettings-ShowAddNewRecordButton="false" CommandItemDisplay="Top" DataSourceID="SqlDataSource1">
                    <Columns>
                        <telerik:GridBoundColumn DataField="SequencialNumber" HeaderText="SequencialNumber"
                            UniqueName="SequencialNumber" SortExpression="SequencialNumber">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Priority" HeaderText="Priority" UniqueName="Priority"
                            FilterControlAltText="Filter Priority column" SortExpression="Priority" DataType="System.Int32">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Process" HeaderText="Staging" UniqueName="Process"
                            SortExpression="Process" FilterControlAltText="Filter Process column">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="SupplierCode" HeaderText="SupplierCode" UniqueName="SupplierCode"
                            SortExpression="SupplierCode" FilterControlAltText="Filter SupplierCode column">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="MessageStatus" HeaderText="MessageStatus" UniqueName="MessageStatus"
                            SortExpression="MessageStatus" FilterControlAltText="Filter MessageStatus column">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="DocumentType" HeaderText="DocumentType" UniqueName="DocumentType"
                            FilterControlAltText="Filter DocumentType column" SortExpression="DocumentType">
                        </telerik:GridBoundColumn>
                        <telerik:GridDateTimeColumn UniqueName="InvoiceCreationDate" DataField="InvoiceCreationDate"
                            HeaderText="InvoiceCreationDate" FilterControlAltText="Filter InvoiceCreationDate column"
                            SortExpression="InvoiceCreationDate">
                            <FilterTemplate>
                                <telerik:RadDatePicker ID="RadDatePicker1" runat="server">
                                </telerik:RadDatePicker>
                            </FilterTemplate>
                        </telerik:GridDateTimeColumn>
    <telerik:GridBoundColumn DataField="SupplierVatNumber" FilterControlAltText="Filter SupplierVatNumber column"
                            HeaderText="SupplierVatNumber" SortExpression="SupplierVatNumber" UniqueName="SupplierVatNumber">
                        </telerik:GridBoundColumn>
                    </Columns>
    <ExpandCollapseColumn Visible="False">
                        <HeaderStyle Width="19px"></HeaderStyle>
                    </ExpandCollapseColumn>
                    <RowIndicatorColumn Visible="False">
                        <HeaderStyle Width="20px" />
                    </RowIndicatorColumn>
                </MasterTableView>
                <FilterMenu EnableImageSprites="False">
                </FilterMenu>
                <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                </HeaderContextMenu>
            </telerik:RadGrid>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EscalonamentoFacturasConnectionString %>"
                SelectCommand="SELECT [SequencialNumber], [Priority],[Process], [SupplierCode], [MessageStatus], [DocumentType],[InvoiceCreationDate], [SupplierVatNumber] FROM [SapDocuments]
    WHERE [Process] IN ('S','SP');"></asp:SqlDataSource>
  8. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 05 Aug 2011 Link to this post

    Hello Ricardo ,

    <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowPaging="True"
        AllowSorting="True" AutoGenerateColumns="False" Width="97%" enableajax="True"
        CssClass="productsGrid" CellSpacing="0" DataSourceID="SqlDataSource1" AllowFilteringByColumn="True"
        ShowFooter="True" Skin="Black" onitemcommand="RadGrid1_ItemCommand">
        <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
        <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
            <Selecting AllowRowSelect="True" />
        </ClientSettings>
        <MasterTableView GridLines="None" Width="100%" CommandItemSettings-ShowExportToCsvButton="True"
            CommandItemSettings-ShowAddNewRecordButton="false" CommandItemDisplay="Top" DataSourceID="SqlDataSource1">
            <Columns>
                <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID" SortExpression="ID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name" FilterControlAltText="Name"
                    SortExpression="PrioNamerity">
                </telerik:GridBoundColumn>
            </Columns>
            <ExpandCollapseColumn Visible="False">
                <HeaderStyle Width="19px"></HeaderStyle>
            </ExpandCollapseColumn>
            <RowIndicatorColumn Visible="False">
                <HeaderStyle Width="20px" />
            </RowIndicatorColumn>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
        </HeaderContextMenu>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT [ID], [Name] FROM [TelerikTable1]"></asp:SqlDataSource>

    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
            {
                if (e.CommandName == RadGrid.ExportToCsvCommandName)
                {
                    RadGrid1.MasterTableView.Columns.FindByUniqueName("ID").Visible = false;
                    RadGrid1.ExportSettings.IgnorePaging = true;
                }
            }

    Let me know if any concern.

    Thanks,
    Jayesh Goyani
  9. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 05 Aug 2011 Link to this post

    Hello again, and thanks for you help...could you tell explain me the changes you did?
  10. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 05 Aug 2011 Link to this post

    Hello,

    i removed below 2 line code because export command automatically bind datasource again when we used needDataSource or SqldataSource as for binding the datasource.

    RadGrid1.DataSource = this.SapDocuments; // please set data source again.
                    RadGrid1.DataBind();

    Please see below link.
    http://www.telerik.com/help/aspnet-ajax/grid-commands-that-invoke-rebind-implicitly.html



    Thanks,
    Jayesh Goyani
  11. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 05 Aug 2011 Link to this post

    Thanks but now it export all the columns data, but i only want to export the first column "SequencialNumber"...
  12. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 05 Aug 2011 Link to this post

    SOLVED!
  13. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 05 Aug 2011 Link to this post

    Hello again...

    It's possible to format the value of the column i wanto to export??

    I have values like "200800000109215" and on the .csv it appears like :
    2,01E+14


    It's possible to export like 200800000109215??
  14. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 06 Aug 2011 Link to this post

    Hello,

    please try below code.

    protected void RadGrid1_GridExporting(object sender, GridExportingArgs e)
       {  
           e.ExportOutput = e.ExportOutput.Replace("\"\r\n\"", "\"\r\n\"'");
       }


    Thanks,
    Jayesh Goyani
  15. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 08 Aug 2011 Link to this post

    HELLO Jayesh,

    Wich are the name of that event?? I'm trying to find it on RadGrid Properties/Events and i can't find it....

  16. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 08 Aug 2011 Link to this post

    Hello Jayesh,

    your code works but i have a "'" before the number
    EX:

    '201100000384244

    I tried :
    protected void RadGrid1_GridExporting(object sender, GridExportingArgs e)
            {
                e.ExportOutput = e.ExportOutput.Replace("\"\r\n\"", "\"\r\n\"");
            }


    but them the number is again :

    2,01E+14

  17. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 08 Aug 2011 Link to this post

    Hello,

    please see attached image.

    Thanks,
    Jayesh Goyani
  18. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 08 Aug 2011 Link to this post

    Hello Ricardo,

    You have to put  ( ' ) symbol because in Csv file if you add this symbol then it will convert your value/text/Number to string.

    Because we are not able to add formatstring on export of CSV so.

    Let me know if any concern.

    for more information please see below link.
    http://www.telerik.com/help/aspnet-ajax/grid-csv-export.html
    http://www.telerik.com/help/aspnet-ajax/grid-html-export.html

    Thanks,
    Jayesh Goyani
  19. Ricardo
    Ricardo avatar
    113 posts
    Member since:
    Jul 2011

    Posted 08 Aug 2011 Link to this post

    ALright...But if we export to excell??
  20. Yogesh
    Yogesh avatar
    2 posts
    Member since:
    Nov 2010

    Posted 18 Apr 2012 Link to this post

    Hi,
    We are using all the export features i.e. Excel, word, Csv and PDF through javascript.
    By default we get output for Excel, PDF and Word but in case of CSV we only get the headers.
    When we set the "ExportOnlyData" property to false in the aspx, we get values in CSV too.
    But, when we do this, the output of other three formats get distorted. We start getting the filter box in the output of Excel.

    Is there a way out so that the "ExportOnlyData" property is set only for CSV?
    We do not intend to move this code to the .cs file.

    Thanks & Regards

    Yogesh Lotlikar
  21. soumen
    soumen avatar
    5 posts
    Member since:
    Aug 2015

    Posted 12 Aug 2015 Link to this post

    I have a radgrid which has an image column.When I am exporting in excel format the image is showing in excel file,but when I am exporting it to csv file it is only showing the image url instead of the image.Is there anyway to embed the image in csv file?

    Anyone can help me please 

  22. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 12 Aug 2015 in reply to soumen Link to this post

    Hello,

    CSV is a text based file format, where as an images are binary data, the two don't mix well.
    You'd be better of supplying a reference to the images (relative path and name) and bundling them together.

    Thanks,

    Jayesh Goyani

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017