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

RadGrid Hide Column on Display, Show on Export

5 Answers 1076 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rebecca Rasmussen
Top achievements
Rank 2
Rebecca Rasmussen asked on 10 Nov 2009, 10:16 PM
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!

5 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 11 Nov 2009, 11:23 AM
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.
0
Rebecca Rasmussen
Top achievements
Rank 2
answered on 11 Nov 2009, 01:21 PM
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? :)

0
Princy
Top achievements
Rank 2
answered on 12 Nov 2009, 11:25 AM
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.
0
Mark
Top achievements
Rank 1
answered on 19 Feb 2014, 03:53 PM
Is there a way to do this in ASP.Net pages?
0
Princy
Top achievements
Rank 2
answered on 20 Feb 2014, 10:32 AM
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
Tags
Grid
Asked by
Rebecca Rasmussen
Top achievements
Rank 2
Answers by
Pavlina
Telerik team
Rebecca Rasmussen
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Mark
Top achievements
Rank 1
Share this question
or