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

Exporting selected RadGrid rows to Excel/CSV

9 Answers 328 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Graham
Top achievements
Rank 1
Graham asked on 14 Mar 2009, 09:58 PM
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.

 

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 16 Mar 2009, 05:25 AM
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.
0
Beth
Top achievements
Rank 1
answered on 23 May 2012, 08:13 PM
Has anyone found a solution to this yet?  I have tried this code and it is not working properly. 
0
Shinu
Top achievements
Rank 1
answered on 24 May 2012, 10:29 AM
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.

0
Simbu
Top achievements
Rank 1
answered on 01 Apr 2013, 11:45 AM
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
0
Kostadin
Telerik team
answered on 04 Apr 2013, 06:38 AM
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.
0
Paul
Top achievements
Rank 1
answered on 31 Oct 2013, 01:32 AM
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.


0
Kostadin
Telerik team
answered on 04 Nov 2013, 12:20 PM
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.
0
C
Top achievements
Rank 1
answered on 15 Nov 2013, 04:47 PM
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!
0
Princy
Top achievements
Rank 1
answered on 18 Nov 2013, 09:53 AM
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
Tags
Grid
Asked by
Graham
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Beth
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Simbu
Top achievements
Rank 1
Kostadin
Telerik team
Paul
Top achievements
Rank 1
C
Top achievements
Rank 1
Share this question
or