Thursday, 26 February 2015

RDLC Report Viewer: Hide (Disable) specific export option (Word / Excel / PDF) from Export button

RDLC  Report Viewer: Hide (Disable) specific export option (Word / Excel / PDF) from Export Button :

 We will be using ReportViewer control’s OnLoad event handler and hence we need to assign an OnLoad event handler to the ReportViewer control as shown below.

<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="650"                  OnLoad="ReportViewer1_OnLoad">                         
</rsweb:ReportViewer>

Then inside the event handler we will write code to hide the Export option in the export button DropDown.
All the export options in ReportViewer control are available through the ListRenderingExtentionsList method. From this list we need to find the extension of the Export option we wish to hide, thus I have done that by matching the extension name in the Lambda expression query.
Once the extension is found we need to access its m_isVisible property using Reflection and set it to false.


Write the code in C# Page:

protected void ReportViewer1_OnLoad(object sender, EventArgs e)
{
     string exportOption = "Excel";
    //string exportOption = "Word";
   // string exportOption = "PDF";
    RenderingExtension extension = ReportViewer1.LocalReport.ListRenderingExtensions().ToList().Find(x => x.Name.Equals(exportOption, StringComparison.CurrentCultureIgnoreCase));
    if (extension != null)
    {
        System.Reflection.FieldInfo fieldInfo = extension.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        fieldInfo.SetValue(extension, false);
    }
}