Export To Excel from RadGrid

10 posts, 1 answers
  1. Gino Suarez
    Gino Suarez avatar
    17 posts
    Member since:
    Oct 2009

    Posted 05 Nov 2009 Link to this post

    (Q2 2009 version)
    Hi, i'm triying to export to excel from my radgrid from buttons on <commandItemTemplate>  tags like this

    <MasterTableView CommandItemDisplay="Top" CellSpacing="0" ShowGroupFooter="true" AllowMultiColumnSorting="true" ShowFooter="true" DataKeyNames="NombreTipoCuenta" >                                                                                     
                                            <CommandItemTemplate> 
                                                <asp:ImageButton runat="server" ID="btnExcelExport" ImageUrl="~/images/page_excel.png" OnClick="btnExcelExport_Click" AlternateText="Excel" ToolTip="Excel" />  
     
                                                                                                                                    
                                            </CommandItemTemplate> 

    but when i click on my button, commanditem hides, and doesn`t work , when i copy this outside the grid it works

    <asp:ImageButton runat="server" ID="btnExcelExport" OnClick="btnExcel_Click" ImageUrl="~/images/page_excel.png"  AlternateText="Excel" />  
                                     <telerik:RadGrid   
                                                AutoGenerateColumns="False"  
                                                ID="tk_Grid"  
                                                AllowPaging="True"  
                                                PageSize="20"  
                                                runat="server"  
                                                GridLines="None" 
                                                ShowFooter="false"  
                                                ShowGroupPanel="false" 
                                                style="margin-top: 0px"  
                                                BorderStyle="None"                                              
                                                CssClass="Grid" onitemdatabound="tk_Grid_ItemDataBound" Skin="Default" 
                                                >                                                                      

    this is the code behind for both mehods
     protected void btnExcelExport_Click(object sender, ImageClickEventArgs e) 
        { 
     
            tk_Grid.ExportSettings.ExportOnlyData = true
            tk_Grid.ExportSettings.IgnorePaging = true
            tk_Grid.ExportSettings.OpenInNewWindow = false
            tk_Grid.MasterTableView.ExportToExcel(); 
        } 

    thanks, Gino.




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

    Posted 06 Nov 2009 Link to this post

    Hello Gino,

    Try setting the CommandName of the ImageButton when it is placed in the CommandItemTemplate and see if it helps:
    aspx:
    <CommandItemTemplate>  
          <asp:ImageButton runat="server" ID="btnExcelExport" ImageUrl="~/images/page_excel.png" CommandName="ExportToExcel" AlternateText="Excel" ToolTip="Excel" />                          
    </CommandItemTemplate>  

    c#:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if (e.CommandName == RadGrid.ExportToExcelCommandName) 
            { 
                RadGrid1.ExportSettings.ExportOnlyData = true
                RadGrid1.ExportSettings.IgnorePaging = true
                RadGrid1.ExportSettings.OpenInNewWindow = false
            } 
        } 

    Hope this helps.
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Gino Suarez
    Gino Suarez avatar
    17 posts
    Member since:
    Oct 2009

    Posted 06 Nov 2009 Link to this post

    Hello Princy, thanks for your answer but it doesn`t work too, occurs the same thing, buttons on CommandItemTemplate hides and don`t show the export dialog.

    Regards Gino.
  5. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 06 Nov 2009 Link to this post

    Hello Gino,

    I am sending you a simple working project which handles the desired functionality. Please give ita try and let me know if it works for you.

    I hope this helps.

    Kind regards,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Gino Suarez
    Gino Suarez avatar
    17 posts
    Member since:
    Oct 2009

    Posted 10 Nov 2009 Link to this post

    Hello Pavlina,

    I try your sample app, and it works, thanks, however, after the first exceution of the export method my links items in the RadGrid doesn`t work and i'm force to reload to restore the functionality. Code below shows how i populate my grid and turn each items into links. I hope you can help me, Thanks.

    protected void tk_Grid_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
    if (e.Item is GridDataItem) 
            { 
                GridDataItem dataItem = (GridDataItem)e.Item; 
     
     
                HyperLink h0 = new HyperLink();             
                currUrl = "frmAnexoBalance.aspx?Periodo=APERTURA&MesID=0&TCID=0&GGPPID=" + dataItem["FormatoGananciaPerdidaID"].Text.Trim() + "&FBID=0"; 
                h0.NavigateUrl = "#"
                if (double.Parse(dataItem["Apertura"].Text) == 0) dataItem["Apertura"].Text = ""
                h0.Text = dataItem["Apertura"].Text; 
                h0.CssClass = "gridtext"
                h0.Attributes.Add("OnClick", "return AbreVentana('" + currUrl + "','SGH Perú');"); 
                dataItem["Apertura"].Controls.Add(h0); 

  7. Answer
    Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 11 Nov 2009 Link to this post

    Hello Gino,

    Based on this information, it is hard to determine what is causing the unwanted behavior. If the issue persists, you can open a formal support ticket, and send us the problematic code, for further investigation.

    Sincerely yours,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  8. Gino Suarez
    Gino Suarez avatar
    17 posts
    Member since:
    Oct 2009

    Posted 11 Nov 2009 Link to this post

    Hi Pavlina,

    I think this problem have a relation with the ajax-enable=false 'cause is the new code i use accords your sample, however i open a formal support ticket, thanks.

    Best wishes
    Gino
  9. THANGARAI
    THANGARAI avatar
    21 posts
    Member since:
    Nov 2013

    Posted 03 Dec 2013 Link to this post

     <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="Images/Excel_ExcelML.png"
                OnClick="ImageButton2_Click" AlternateText="ExcelML" />
    ------------------------------------------
    Protected
    Sub ImageButton2_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton2.Click
               
               Rg_StdCompReferrals.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML 
             Rg_StdCompReferrals.ExportSettings.IgnorePaging = True
               Rg_StdCompReferrals.ExportSettings.ExportOnlyData = True
               Rg_StdCompReferrals.ExportSettings.OpenInNewWindow = True
               Rg_StdCompReferrals.MasterTableView.ExportToExcel()
           End Sub
     
    Hi princy,
    Export to excel is not working.
    Please help me to overcome this problem
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Dec 2013 Link to this post

    Hi THANGARAI,

    I guess your grid might be Ajaxified. The exporting feature works only with regular postbacks. This means, that the asynchronous postback should be canceled when performing an export. More information can be found here. Please try the following code snippet.

    ASPX:
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ClientEvents OnRequestStart="onRequestStart"></ClientEvents>
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="Rg_StdCompReferrals">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="Rg_StdCompReferrals"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>

    JS:
    <script type="text/javascript">
     function onRequestStart(sender, args) {
        if (args.get_eventTarget().indexOf("ImageButton2") >= 0) {
            args.set_enableAjax(false);
        }
     }
    </script>

    Thanks,
    Princy

  11. THANGARAI
    THANGARAI avatar
    21 posts
    Member since:
    Nov 2013

    Posted 03 Dec 2013 Link to this post

    Hi princy,
    Thank you so much.  Its working great.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017