Hide columns before export to excel

8 posts, 0 answers
  1. Felipe Delgado
    Felipe Delgado avatar
    2 posts
    Member since:
    Sep 2012

    Posted 16 Oct 2009 Link to this post

    Hi!
    I am using an export button within the toolbar of my radgrid, and it's work very good, but now I need to hide some columns and show others before export the content of the radgrid to excel. Can somebody tell me how to do it?

    Thanks so much.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Oct 2009 Link to this post

    Hi,

    You can try out the following code to hide columns in the grid before exporting:
    c#:
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) 
        { 
            if (e.CommandName == RadGrid.ExportToExcelCommandName) 
            {            
                RadGrid1.MasterTableView.GetColumn("ColumnUniqueName").Visible = false
            } 
        } 

    Thanks
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Felipe Delgado
    Felipe Delgado avatar
    2 posts
    Member since:
    Sep 2012

    Posted 19 Oct 2009 Link to this post

    Thanks a lot Princy, it's really work!

    Do you know if I can use the GridExporting Event instead of Item Command?

    Thank you again.
  5. Karan
    Karan avatar
    14 posts
    Member since:
    Feb 2008

    Posted 04 Feb 2013 Link to this post

    This hides the column in the export alright, but after the export the columns are also hidden in the browser view.

    Is there a way to only hide the columns for excel export but keep them visible in the browser ?

    Thanks
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Feb 2013 Link to this post

    Hi,

    Unfortunately I cannot replicate the issue at my end. Here is the full code that I tried which worked as expected.
    aspx:
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource2" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand">
     <MasterTableView  CommandItemDisplay="Top">
        <CommandItemSettings ShowExportToExcelButton="true" />
           <Columns>
             <telerik:GridBoundColumn DataField="ShippedDate" UniqueName="ShippedDate"></telerik:GridBoundColumn>
             <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID"></telerik:GridBoundColumn>
           </Columns>
     </MasterTableView>
    </telerik:RadGrid>
    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
      if (e.CommandName == RadGrid.ExportToExcelCommandName)
      {
                RadGrid1.MasterTableView.GetColumn("OrderID").Visible = false;
      }
    }

    Please elaborate your scenario if it doesn't help.

    Thanks,
    Princy
  7. Karan
    Karan avatar
    14 posts
    Member since:
    Feb 2008

    Posted 05 Feb 2013 Link to this post

    Thanks for the reply Princy, The problem was with our cache that was storing the columns. We were reusing the columns hence setting the visible = false means that next time when we use that column it would not render in the browser. We have fixed this problem.

    We are still having a problem with the Template column.  Is there a way to change the temple column cell data in the exported excel file.

    For eg. I have a template column with an Image and a hyperlink next to it.  But when I export this to excel I just want to see the hyperlink and not the image. At the moment the default export just generates a blank cell.  If possible please post an example.

    Thanks for your help.
  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Feb 2013 Link to this post

    Hi,

    Try the following code to achieve your scenario.
    C#:
      protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
             if (e.CommandName == RadGrid.ExportToExcelCommandName)
            {
                foreach (GridDataItem item in RadGrid1.Items)
                {
                    Image img = (Image)item.FindControl("Image1");
                    img.Visible = false;
                    HyperLink link = (HyperLink)item.FindControl("HyperLink1");
                    link.Visible = true;
                }
             }
    }

    Thanks,
    Princy
  9. Paul
    Paul avatar
    2 posts
    Member since:
    Mar 2015

    Posted 19 Oct 2015 in reply to Princy Link to this post

    Princy:

     You Rock!

    - Paul

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017