Radgrid; export only selected rows

16 posts, 0 answers
  1. Frank
    Frank avatar
    18 posts
    Member since:
    Sep 2008

    Posted 29 May 2009 Link to this post

    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

  2. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 29 May 2009 Link to this post

    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.
  3. Frank
    Frank avatar
    18 posts
    Member since:
    Sep 2008

    Posted 02 Jun 2009 Link to this post

    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
  4. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 05 Jun 2009 Link to this post

    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.
  5. Frank
    Frank avatar
    18 posts
    Member since:
    Sep 2008

    Posted 08 Jun 2009 Link to this post

    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


  6. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 11 Jun 2009 Link to this post

    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.
  7. Marco
    Marco avatar
    5 posts
    Member since:
    Jul 2010

    Posted 09 Mar 2011 Link to this post

    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?
  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 10 Mar 2011 Link to this post

    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.
  9. Marco
    Marco avatar
    5 posts
    Member since:
    Jul 2010

    Posted 10 Mar 2011 Link to this post

    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.
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 14 Mar 2011 Link to this post

    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.
  11. Gibin
    Gibin avatar
    2 posts
    Member since:
    Jun 2010

    Posted 28 Aug 2012 Link to this post

    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.
  12. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 03 Sep 2012 Link to this post

    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.
  13. Gibin
    Gibin avatar
    2 posts
    Member since:
    Jun 2010

    Posted 03 Sep 2012 Link to this post

    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
  14. brian
    brian avatar
    36 posts
    Member since:
    Jul 2010

    Posted 18 Jun 2013 Link to this post

    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.
  15. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 21 Jun 2013 Link to this post

    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.
  16. Muhammad
    Muhammad avatar
    1 posts
    Member since:
    Oct 2017

    Posted 18 Jun 2018 Link to this post

    how to export only visible columns in radgridview to excel in c#
Back to Top