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

Radgrid; export only selected rows

15 Answers 1051 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 2
Frank asked on 29 May 2009, 01:23 PM
Hi all,

- I really like the export to excel and export to csv -functions:
http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/exporting/defaultcs.aspx

- Also, I really like the Grid / Client-side Row Selection:
http://demos.telerik.com/aspnet-ajax/grid/examples/client/selecting/defaultcs.aspx

But how can I combine the 2 things?

I wonder why no one have thought of it before?
- I want to be able to select specific items from my grid, and ONLY export the selected rows to Excel and/or CSV.

How can this be done?

If there is a post for it already, then - sorry - I have tried to find it... without any luck.

Thanks a lot.

BR,
Frank

15 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 29 May 2009, 02:24 PM
Hello Frank,

Please examine the following links:
Exporting only selected rows
Exporting selected RadGrid rows to ...

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Frank
Top achievements
Rank 2
answered on 02 Jun 2009, 07:29 AM
Thanks a lot, it works fine to make the rows invisible, before exporting.
BUT it doesn't seem to work, when using Excel -format: 'GridExcelExportFormat.ExcelML'

When using this format, all rows are exported.

Any ideas?

Thanks,
Frank
0
Daniel
Telerik team
answered on 05 Jun 2009, 02:56 PM
Hello Frank,

My suggestion is valid for the HTML Excel type only. The reason is that the ExcelML engine gets the data straight from the datasource and it doesn't care if the rows are hidden or not .

For ExcelML you should use another approach to remove rows:
protected void RadGrid1_ExcelMLExportRowCreated(object source, GridExportExcelMLRowCreatedArgs e) 
    if (yourCondition && e.RowType == GridExportExcelMLRowType.DataRow) 
        e.Worksheet.Table.Rows.Remove(e.Row); 

Kind regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Frank
Top achievements
Rank 2
answered on 08 Jun 2009, 09:04 AM
Hello again,

Thanks for your post. I understand that you remove the rows before exporting them, but still I CANT get the code to work.

I code in VB, and it seems like I cant get the OnExcelMLExportRowCreated -event fired.

Look at my code, whats wrong?

Design:
<telerik:RadGrid ID="RadGrid2" OnExcelMLExportRowCreated="RadGrid2_ExcelMLExportRowCreated" 
                        AutoGenerateColumns="false" runat="server" AllowPaging="True" DataSourceID="SqlDataSource2" 
                        GridLines="None" AllowMultiRowSelection="true" PageSize="20" AllowFilteringByColumn="true" 
                        AllowSorting="True" Skin="Web20" ShowStatusBar="true"

<div> 
                            <asp:LinkButton ID="btn_exportExcelML" runat="server" Text="Export to Excel" /></div


code behind:
 Protected Sub btn_exportExcelML_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_exportExcelML.Click 
        ConfigureExport() 
 
        RadGrid2.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML 
        RadGrid2.MasterTableView.ExportToExcel() 
    End Sub 
 
    Protected Sub RadGrid2_ExcelMLExportRowCreated(ByVal source As Object, ByVal e As GridExportExcelMLRowCreatedArgs) 
 
        If RadGrid2.SelectedItems.Count <> 0 Then 
            For Each item As GridDataItem In RadGrid2.MasterTableView.Items 
 
                If Not item.Selected AndAlso e.RowType = GridExportExcelMLRowType.DataRow Then 
                    e.Worksheet.Table.Rows.Remove(e.Row) 
                End If 
            Next 
        Else 
            RadGrid2.ExportSettings.IgnorePaging = True 
        End If 
 
    End Sub 
 
 
    Public Sub ConfigureExport() 
        With RadGrid2.ExportSettings 
            .ExportOnlyData = True 
 
            .OpenInNewWindow = True 
            .FileName = "Website leads " & Trim(Me.ddSelectDate.SelectedItem.ToString()) 
        End With 
    End Sub 

Thanks.

Frank


0
Daniel
Telerik team
answered on 11 Jun 2009, 03:26 PM
Hello Frank,

I attached a simple demo to this post. You can easily modify it to expand the selected items only.

Hope this helps.

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Marco
Top achievements
Rank 1
answered on 09 Mar 2011, 04:40 PM
Hi Daniel,
Thanks for your help.  I tried to follow the example that you show here.  My problem now is in the for each radgrid.dataitem loop.  The dataitem.Selected property seems to be always false even if I selected the row.  Any thoughts?
0
Princy
Top achievements
Rank 2
answered on 10 Mar 2011, 09:41 AM
Hello Macro,

Can you please verify that the viewstate for the grid is enabled. If not set it as true and  see if it works now.

Thanks,
Princy.
0
Marco
Top achievements
Rank 1
answered on 10 Mar 2011, 04:01 PM
Hi Princy, I have the Viewstate enabled for the grid.  I'm stumped.  Here's the Radgrid I'm using.  But I must say that the code that removes rows from the excel export using ExcelML works. 

<telerik:RadGrid ID="radReceipts" runat="server" AllowFilteringByColumn="true" GridLines="None"
                        Skin="Vista" AllowPaging="True" AllowSorting="True" EnableLinqExpressions="False"
                        AutoGenerateColumns="False" AllowMultiRowSelection="True" EnableViewState="true">
                        <MasterTableView AutoGenerateColumns="False" ClientDataKeyNames="ReceiptID" DataKeyNames="ReceiptID"
                            AllowFilteringByColumn="True" EnableViewState="True">
                            <RowIndicatorColumn Visible="False">
                                <HeaderStyle Width="20px"></HeaderStyle>
                            </RowIndicatorColumn>
                            <ExpandCollapseColumn Visible="False" Resizable="False">
                                <HeaderStyle Width="20px"></HeaderStyle>
                            </ExpandCollapseColumn>
                            <Columns>
                                <telerik:GridClientSelectColumn HeaderStyle-Width="20px" UniqueName="Checkbox">
                                    <HeaderStyle Width="20px" />
                                </telerik:GridClientSelectColumn>
                                <telerik:GridBoundColumn UniqueName="ReceiptID" HeaderText="Receipt ID" DataField="ReceiptID"
                                    Display="False">
                                </telerik:GridBoundColumn>
                                <telerik:GridHyperLinkColumn AllowFiltering="true" UniqueName="Credited To" DataTextField="CreditedTo"
                                    HeaderText="Credited To" DataTextFormatString="{0}" DataNavigateUrlFields="ReceiptID"
                                    DataNavigateUrlFormatString="javascript:open_receipt({0});" DataType="System.String"
                                    SortExpression="CreditedTo">
                                </telerik:GridHyperLinkColumn>
                                <telerik:GridBoundColumn Display="false" UniqueName="CreditedToOnly" DataField="CreditedTo"
                                    DataType="System.String" DataFormatString="{0}" HeaderText="Credited To">
                                </telerik:GridBoundColumn>
                                <telerik:GridHyperLinkColumn AllowFiltering="true" UniqueName="Receipt Level" DataTextField="ReceiptLevel"
                                    HeaderText="Receipt Level" DataTextFormatString="{0}" DataType="System.String"
                                    DataNavigateUrlFields="ReceiptID" DataNavigateUrlFormatString="javascript:open_receipt({0});"
                                    SortExpression="ReceiptLevel">
                                </telerik:GridHyperLinkColumn>
                                <telerik:GridBoundColumn UniqueName="ReceiptLevelOnly" DataField="ReceiptLevel" Display="false"
                                    HeaderText="Receipt Level">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Campaign Name" DataField="CampaignName" DataType="System.String"
                                    HeaderText="Campaign">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Receipt Category" DataField="ReceiptCategory"
                                    HeaderText="Receipt Category">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Receipt Type" DataField="ReceiptType" HeaderText="Receipt Type">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Event" DataField="EventSiteName" HeaderText="Event Site">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Total Receipt Amount" DataField="ReceiptAmountTotal"
                                    HeaderText="Receipt Total" DataType="System.Double" DataFormatString="{0:c}">
                                    <ItemStyle Width="10px" />
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Total Donor Amount" DataType="System.Double"
                                    DataField="DonorAmountTotal" HeaderText="Donor Total" DataFormatString="{0:c}">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Expected Matching Gift" DataType="System.Double"
                                    DataField="ExpMatchingGift" HeaderText="Expecting MG$" DataFormatString="{0:c}">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Incentive Distributed" DataField="IncentiveDistributed"
                                    HeaderText="Incentive Distributed">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn UniqueName="Follow Up" DataField="FollowUp" HeaderText="Follow Up">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="Comment" HeaderText="Comments" UniqueName="Comments"
                                    Visible="true" Display="false">
                                </telerik:GridBoundColumn>
                                <telerik:GridEditCommandColumn EditText="+" UniqueName="EditCommandColumn" HeaderText="Comment">
                                </telerik:GridEditCommandColumn>
                                <telerik:GridBoundColumn DataField="JdeClosingDate" DataFormatString="{0:MM/dd/yyyy}"
                                    Display="False" HeaderText="JDE Closing Date" UniqueName="JDE Closing Date">
                                </telerik:GridBoundColumn>
                            </Columns>
                            <EditFormSettings EditFormType="Template">
                                <EditColumn UniqueName="commentsColumnEx" UpdateText="" CancelText="Close" Display="true">
                                </EditColumn>
                                <FormTemplate>
                                    <table width="85%">
                                        <tr>
                                            <td>
                                                <b>Comments: </b>   
                                                <asp:LinkButton ID="btnCancelComments" Text="close" runat="server" CausesValidation="False"
                                                    CommandName="Cancel"></asp:LinkButton>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                <textarea id="labelComments" cols="120" rows="5" readonly="readonly"><%# DataBinder.Eval(Container.DataItem, "Comment") %></textarea>
                                            </td>
                                        </tr>
                                    </table>
                                </FormTemplate>
                            </EditFormSettings>
                        </MasterTableView>
                        <ClientSettings>
                            <Selecting AllowRowSelect="True" />
                            <ClientEvents OnGridCreated="on_grid_created" OnFilterMenuShowing="on_show_filter"
                                OnColumnClick="on_column_click" OnRowCreated="on_row_created" OnRowSelected="on_row_selected"
                                OnRowDeselected="on_row_deselected" />
                        </ClientSettings>
                        <FilterMenu Skin="Vista" EnableTheming="True" Width="30px">
                            <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
                        </FilterMenu>
                    </telerik:RadGrid>

Now I'm just struggling to make the selected property to work.  Here's the code behind where the dataitem.Selected always returns as False:

Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
    Try
        With radReceipts
            Dim list As New Generic.List(Of String)()
            For Each dataitem As GridDataItem In .MasterTableView.Items
                If dataitem.Selected Then
                    list.Add(dataitem("ReceiptID").Text)
                End If
            Next
            Session("persistExpanded") = list
            .ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.ExcelML
            .MasterTableView.Columns.FindByUniqueName("CreditedToOnly").Display = True
            .MasterTableView.Columns.FindByUniqueName("ReceiptLevelOnly").Display = True
            .ExportSettings.IgnorePaging = True
            .ExportSettings.ExportOnlyData = True
            .ExportSettings.OpenInNewWindow = True
            .MasterTableView.ExportToExcel()
        End With
    Catch ex As Exception
        Dim errLog As New ErrorLogger()
        errLog.StoreError("ExcelView", "ExportToExcel", ex.Message)
        Response.Redirect("ErrorPage.aspx?ErrorMessage=Failed Export Excel:" + ex.Message)
    End Try
End Sub

Thanks for the help.
0
Princy
Top achievements
Rank 2
answered on 14 Mar 2011, 11:39 AM
Hello Macro,

Can you try to bind the grid using advanced binding through the NeedDataSource event  as shown in this demos:
Grid / Advanced Data Binding

Thanks,
Princy.
0
Gibin
Top achievements
Rank 1
answered on 28 Aug 2012, 08:38 AM
I also faced a similar issue while exporting. Even If I made the griditem visible o false it was exporting . I have noticed that when the export button is clicked the need datasource is also firing. I have added one flag in my code so that it will not execute the need datasource if I click the export button. Now I am able to export with the selected rows.
0
Daniel
Telerik team
answered on 03 Sep 2012, 12:03 PM
Hello Gibin,

You'd like to export only the items that are selected, right? Try the attached demo if this is so.
This approach should work for other formats, too.

Regards,
Daniel
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
Gibin
Top achievements
Rank 1
answered on 03 Sep 2012, 12:08 PM
Thanks Daniel.
The issue I was facing has been resolved by blocking the code inside the needdatasource , after I am clicking the export button.

Regards
Gibin
0
brian
Top achievements
Rank 1
answered on 18 Jun 2013, 11:48 PM
The example provided will be poor performing if you have a lot of records and you only want to export the selected rows.   For some reason, the export of selected rows was working fine but then it stopped and went to exporting all rows in datasource.   I'm using excel html.    I think the better approach is to not rebind the grid somehow.
0
Daniel
Telerik team
answered on 21 Jun 2013, 10:34 AM
Hello Brian,

Better performing solution would be to filter your datasource with the selected items. Thus RadGrid will not have to create the rest of the items at all.
Even faster approach would be not to use the built-in export and instead query your database directly and filter the results with the selected items and finally use the export infrastructure to directly generate the Excel file.

Regards,
Daniel
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
Muhammad
Top achievements
Rank 1
answered on 18 Jun 2018, 11:05 AM
how to export only visible columns in radgridview to excel in c#
Tags
Grid
Asked by
Frank
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Frank
Top achievements
Rank 2
Marco
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Gibin
Top achievements
Rank 1
brian
Top achievements
Rank 1
Muhammad
Top achievements
Rank 1
Share this question
or