Exporting selected RadGrid rows to Excel/CSV

10 posts, 0 answers
  1. Graham
    Graham avatar
    7 posts
    Member since:
    Feb 2008

    Posted 14 Mar 2009 Link to this post

    I have a RadGrid which I can export as an Excel (or CSV) file using MasterTableView.ExportToExcel.

    The RadGrid has a GridClientSelectColumn - how can I export only selected (checked) rows to CSV/Excel?

    If no rows are selected, then I also want to be able to export the whole grid / dataset.

    Thanks,

    Graham.

     

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Mar 2009 Link to this post

    Hello Graham,

    Try the following code snippets for exporting selected rows of the RadGrid and also exporting complete grid data when no rows selected.

    CS:
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        if (RadGrid1.SelectedItems.Count != 0)  
        {  
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)  
            {  
                if (!item.Selected)  
                    item.Visible = false;  
            }  
        }  
        RadGrid1.ExportSettings.OpenInNewWindow = true;  
        RadGrid1.MasterTableView.ExportToPdf();  

    Thanks,
    Princy.
  3. Beth
    Beth avatar
    1 posts
    Member since:
    May 2012

    Posted 23 May 2012 Link to this post

    Has anyone found a solution to this yet?  I have tried this code and it is not working properly. 
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 24 May 2012 Link to this post

    Hello Beth,

    The above code is working fine at my end with Excel-Format="Html" and exporting to pdf. If the Excel-Format is "ExcelML" it gets the data straight from the datasource and it doesn't care if the rows are hidden or not . Here I am pasting the complete code which worked as expected.
    aspx:
    <telerik:RadGrid AllowPaging="true" AllowMultiRowSelection="true"  ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="false">
     <ExportSettings ExportOnlyData="true" Excel-Format="Html"></ExportSettings>
      <MasterTableView>
       <Columns>
        <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="FirstName"></telerik:GridBoundColumn>
       </Columns>
      </MasterTableView>
     <ClientSettings>
      <Selecting AllowRowSelect="true" />
     </ClientSettings>
    </telerik:RadGrid>
     <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
       foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            if (!item.Selected)
            item.Visible = false;
       }
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.MasterTableView.ExportToExcel();
    }

    Thanks,
    Shinu.

  5. Simbu
    Simbu avatar
    2 posts
    Member since:
    Jan 2013

    Posted 01 Apr 2013 Link to this post

    Hi All, 

      How to export Selected rows from Rad Grid into Word? Could you please anyone help me?

     foreach (GridDataItem item in grdSearchList.MasterTableView.Items)
      {
                       if (!item.Selected)
                           item.Visible = false;
                        item.Style["background-color"] = "#359AFF";
       }
                    grdSearchList.MasterTableView.Columns[0].Visible = false;
                    grdSearchList.MasterTableView.ShowHeadersWhenNoRecords = true;
                    
                    grdSearchList.ExportSettings.ExportOnlyData = true;
                    grdSearchList.ExportSettings.IgnorePaging = false;
                    grdSearchList.ExportSettings.FileName = "Call Report Details";
                    grdSearchList.MasterTableView.ExportToWord();

    Thanks,
    Simbu.M
  6. Kostadin
    Admin
    Kostadin avatar
    1733 posts

    Posted 04 Apr 2013 Link to this post

    Hello Simbu,

    I try your suggested approach and it is working fine on my side. I prepared a small sample based on your code and attached it to this forum post. Please give it a try and let me know how it differs from your real setup.

    Kind regards,
    Kostadin
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  7. Paul
    Paul avatar
    1 posts
    Member since:
    Feb 2012

    Posted 30 Oct 2013 Link to this post

    It's the ExportSettings IgnorePaging setting.

    <ExportSettings IgnorePaging="true"></ExportSettings>

    Set to true, the following code doesn't work, and all rows are exported, regardless of selection.

    foreach (GridDataItem col in rgWVs.MasterTableView.Items)
    {
        if (!col.Selected) col.Visible = false;
    }
    rgWVs.ExportSettings.OpenInNewWindow = true;
    rgWVs.MasterTableView.ExportToExcel();

    Set IgnorePaging to false, and it works.


  8. Kostadin
    Admin
    Kostadin avatar
    1733 posts

    Posted 04 Nov 2013 Link to this post

    Hello Paul,

    You are absolutely correct. This is expecting because when IgnorePaging is set to true, the grid will be rebound and the current selected items will be lost. That is why you have to either disable this property or persist them as it is described in the following help article.

    Regards,
    Kostadin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  9. C
    C avatar
    1 posts
    Member since:
    Nov 2013

    Posted 15 Nov 2013 Link to this post

    Is there a way to export a RadGrid to excel, for only the selected rows...but also for only certain columns?  I've tried setting .visible property to 'False' for those columns I don't want on the Excel spreadsheet, and then using the .orderindex property to order the remaining columns in the correct order, however the results are not what I'm looking for.  Any help you could provide would be much appreciated.  Also, would prefer in VB if possible.  Thanks!
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Nov 2013 Link to this post

    Hi,

    Please try the following code snippet. If this doesn't help, provide your full code snippet.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowSelection="true" OnItemCommand="RadGrid1_ItemCommand">
        <MasterTableView  CommandItemDisplay="Top">
            <CommandItemSettings ShowExportToExcelButton="true" />
            <Columns>
             . . . . .
            </Columns>
        </MasterTableView>
        <ClientSettings Selecting-AllowRowSelect="true">
        </ClientSettings>
    </telerik:RadGrid>

    VB:
    Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
        If e.CommandName = RadGrid.ExportToExcelCommandName Then
            If RadGrid1.SelectedItems.Count <> 0 Then
                For Each item As GridDataItem In RadGrid1.MasterTableView.Items
                    If Not item.Selected Then
                        item.Visible = False
                    End If
                Next
                RadGrid1.MasterTableView.GetColumn("UniqueColumnName").Visible = False
                RadGrid1.ExportSettings.ExportOnlyData = True
                RadGrid1.ExportSettings.IgnorePaging = False
                RadGrid1.ExportSettings.FileName = "Export Grid"
            End If
        End If
    End Sub

    Thanks,
    Princy
Back to Top