RadGrid Hide Column on Display, Show on Export

6 posts, 0 answers
  1. Rebecca Rasmussen
    Rebecca Rasmussen avatar
    20 posts
    Member since:
    Mar 2007

    Posted 10 Nov 2009 Link to this post

    C# .NET 3.5

    Telerik.Web.UI, Version=2008.3.1125.35


    I have a frequent need to be able to hide some columns in a grid on display, but have them available on export and hide others on export. Thus far I've had no luck in being able to do all three. The show on export of the columns we hide for display is the piece that's not working for me.

    I have in the ItemCreated command the hide columns code

     
                    // hide columns  
                    string myColumnsToHide = myDatabaseAccess.v2_sp_application_configuration_matrix_get_value("manage_spoc_matrix""HideOnDisplayResults_RadGrid1");  
                    foreach (Telerik.Web.UI.GridColumn myColumn in RadGrid1.MasterTableView.RenderColumns)  
                    {  
                        if (myColumnsToHide.ToLower().Contains("|" + myColumn.UniqueName.ToLower() + "|"))  
                        {  
                            myColumn.Display = false;  
                        }  
                    } 

    and in the ItemCommand for the export piece the code to show the prev hidden columns then hide addional ones from the export

     
                        // show columns we hid for display  
                        string myColumnsToShow = myDatabaseAccess.v2_sp_application_configuration_matrix_get_value("manage_spoc_matrix""HideOnDisplayResults_RadGrid1");  
                        foreach (Telerik.Web.UI.GridColumn myColumn in RadGrid1.MasterTableView.RenderColumns)  
                        {  
                            if (myColumnsToShow.ToLower().Contains("|" + myColumn.UniqueName.ToLower() + "|"))  
                            {  
                                myColumn.Display = true;  
                            }  
                        }  
     
                        // then hide any coulmns we don't want on export  
                        string myColumnsToHide = myDatabaseAccess.v2_sp_application_configuration_matrix_get_value("manage_spoc_matrix""HideOnExport_RadGrid1");  
                        foreach (Telerik.Web.UI.GridColumn myColumn in RadGrid1.MasterTableView.RenderColumns)  
                        {  
                            if (myColumnsToHide.ToLower().Contains("|" + myColumn.UniqueName.ToLower() + "|"))  
                            {  
                                myColumn.Display = false;  
                            }  
                        } 

    The hide on display works. The hide of the additional columns on export works. But I can't seem to show on export the columns we hid for display purposes. It seems the ItemCreated event is exercised multiple times (as far as I can tell 1x before and 2x after) when invoking the ItemCommand function.

    Any suggestions on how to show on export the columns we want hidden on display?

    Thanks!
  2. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 11 Nov 2009 Link to this post

    Hi Rebecca,

    In order to achieve the desired functionality, please examine the following forum thread which discuss a similar problem
    http://www.telerik.com/community/forums/aspnet-ajax/grid/radgrid-export.aspx

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rebecca Rasmussen
    Rebecca Rasmussen avatar
    20 posts
    Member since:
    Mar 2007

    Posted 11 Nov 2009 Link to this post

    Thanks Pavlina. But unfortunately that does not address my issue. The thread in question discusses only showing the column, does not address the fact that if you're trying to:
    • hide a column for display purposes in the ItemCreated event but
    • show it for export purposes in the ItemCommand event
    • the ItemCreated event gets called and once again hides the columns prior to the actual export.

    That specific scenario is what I'm experiencing and thus needing help with. Perhaps I need to attach the hide and show in different events than the ItemCreated and ItemCommand in order to get around this? But what? When? Why? How? :)

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

    Posted 12 Nov 2009 Link to this post

    Hello Rebecca,

    You can set a global bool variable to true on exporting and in the ItemCreated event of the grid, hide the columns if the variable value is false. Here's a sample code:
    c#:
        bool IsExport = false
        protected void btnExport_Click(object sender, EventArgs e) 
        { 
            IsExport = true
     
            //your code to show columns     
         
            RadGrid1.MasterTableView.ExportToExcel(); 
        } 
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
        { 
            if (!IsExport) 
            { 
                //your code to hide columns     
            } 
       } 

    Hope this helps..
    Princy.
  6. Mark
    Mark avatar
    1 posts
    Member since:
    Feb 2014

    Posted 19 Feb 2014 in reply to Pavlina Link to this post

    Is there a way to do this in ASP.Net pages?
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Feb 2014 in reply to Mark Link to this post

    Hi Mark,

    I'm afraid such a solution is not available from ASPX side. You can set display as false for the column in ASPX side and set its display to true during export in the ItemCommand event. Please take a look at the below code snippet.

    ASPX:
    <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" Display="false">
    </telerik:GridBoundColumn>

    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
      if (e.CommandName == RadGrid.ExportToExcelCommandName)
      {
          RadGrid1.MasterTableView.GetColumn("OrderID").Display = true;
      }
    }

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