Question

Do anyone have good ideas of how to modify the toolbar for the WinForms version of the ReportViewer Toolbar? That is, I want to remove some buttons and varius, but it looks like the solution is to create a brand new toolbar instead of modifying the one that is there.

Like, I had to remove export to excel, and did it this way:

  // Disable excel export
  foreach (RenderingExtension extension in lr.ListRenderingExtensions()) {
    if (extension.Name == "Excel") {
      //extension.Visible = false; // Property is readonly...
      FieldInfo fi = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
      fi.SetValue(extension, false);
    }
  }

A bit trickysh if you ask me.. For removing toolbarbuttons, an possible way was to iterate through the Control array inside the ReportViewer and change the Visible property for the buttons to hide, but it gets reset all the time, so it is not an good way..

WHEN do MS come with an new version btw?

Was it helpful?

Solution

There are a lot of properties to set which buttons would you like to see.

For example ShowBackButton, ShowExportButton, ShowFindControls, and so on. Check them in the help, all starts with "Show".

But you are right, you cannot add new buttons. You have to create your own toolbar in order to do this.

What do you mean about new version? There is already a 2008 SP1 version of it.

OTHER TIPS

Yeap. You can do that in a little tricky way. I had a task to add more scale factors to zoom report. I did it this way:

    private readonly string[] ZOOM_VALUES = { "25%", "50%", "75%", "100%", "110%", "120%", "125%", "130%", "140%", "150%", "175%", "200%", "300%", "400%", "500%" };
    private readonly int DEFAULT_ZOOM = 3;
    //--

    public ucReportViewer()
    {
        InitializeComponent();   
        this.reportViewer1.ProcessingMode = ProcessingMode.Local;

        setScaleFactor(ZOOM_VALUES[DEFAULT_ZOOM]);

        Control[] tb = reportViewer1.Controls.Find("ReportToolBar", true);

        ToolStrip ts;
        if (tb != null && tb.Length > 0 && tb[0].Controls.Count > 0 && (ts = tb[0].Controls[0] as ToolStrip) != null)
        {
            //here we go if our trick works (tested at .NET Framework 2.0.50727 SP1)
            ToolStripComboBox tscb = new ToolStripComboBox();
            tscb.DropDownStyle = ComboBoxStyle.DropDownList;

            tscb.Items.AddRange(ZOOM_VALUES);                
            tscb.SelectedIndex = 3; //100%

            tscb.SelectedIndexChanged += new EventHandler(toolStripZoomPercent_Click);

            ts.Items.Add(tscb);
        }
        else
        {                
            //if there is some problems - just use context menu
            ContextMenuStrip cmZoomMenu = new ContextMenuStrip();

            for (int i = 0; i < ZOOM_VALUES.Length; i++)
            {
                ToolStripMenuItem tsmi = new ToolStripMenuItem(ZOOM_VALUES[i]);

                tsmi.Checked = (i == DEFAULT_ZOOM);
                //tsmi.Tag = (IntPtr)cmZoomMenu;
                tsmi.Click += new EventHandler(toolStripZoomPercent_Click);

                cmZoomMenu.Items.Add(tsmi);
            }

            reportViewer1.ContextMenuStrip = cmZoomMenu;
        }                    
    }

    private bool setScaleFactor(string value)
    {
        try
        {
            int percent = Convert.ToInt32(value.TrimEnd('%'));

            reportViewer1.ZoomMode = ZoomMode.Percent;
            reportViewer1.ZoomPercent = percent;

            return true;
        }
        catch
        {
            return false;
        }
    }


    private void toolStripZoomPercent_Click(object sender, EventArgs e)
    {
        ToolStripMenuItem tsmi = sender as ToolStripMenuItem;
        ToolStripComboBox tscb = sender as ToolStripComboBox;

        if (tscb != null && tscb.SelectedIndex > -1)
        {
            setScaleFactor(tscb.Items[tscb.SelectedIndex].ToString());
        }
        else if (tsmi != null)
        {
            if (setScaleFactor(tsmi.Text))
            {
                foreach (ToolStripItem tsi in tsmi.Owner.Items)
                {
                    ToolStripMenuItem item = tsi as ToolStripMenuItem;

                    if (item != null && item.Checked)
                    {
                        item.Checked = false;
                    }
                }

                tsmi.Checked = true;
            }
            else
            {
                tsmi.Checked = false;
            }
        }
    }

Get the toolbar from ReportViewer control:

ToolStrip toolStrip = (ToolStrip)reportViewer.Controls.Find("toolStrip1", true)[0]

Add new items:

toolStrip.Items.Add(...)

Another way would be to manipulate the generated HTML at runtime via javascript. It's not very elegant, but it does give you full control over the generated HTML.

For VS2013 web ReportViewer V11 (indicated as rv), the code below adds a button.

private void AddPrintBtn()
    {           
        foreach (Control c in rv.Controls)
        {
            foreach (Control c1 in c.Controls)
            {
                foreach (Control c2 in c1.Controls)
                {
                    foreach (Control c3 in c2.Controls)
                    {
                        if (c3.ToString() == "Microsoft.Reporting.WebForms.ToolbarControl")
                        {
                            foreach (Control c4 in c3.Controls)
                            {
                                if (c4.ToString() == "Microsoft.Reporting.WebForms.PageNavigationGroup")
                                {
                                    var btn = new Button();
                                    btn.Text = "Criteria";
                                    btn.ID = "btnFlip";
                                    btn.OnClientClick = "$('#pnl').toggle();";
                                    c4.Controls.Add(btn);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

I had this question for al ong time I I found the answer after a long tie and the main source of kowledge I used was this webpega: I'd like to thank you all guys adding the code that allowed me to do it and a picture with the result.

Instead of using the ReportViewer Class, you need to create a new classs, in my case, I named it ReportViewerPlus and it goes like this:

using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace X
{
    class ReportViewerPlus : ReportViewer
    {
        private Button boton { get; set; }

        public ReportViewerPlus(Button but) {
            this.boton = but;
            testc(this.Controls[0]);
        }
        public ReportViewerPlus()
        {
        }
        private void testc(Control item){
            if(item is ToolStrip)   
            {       
                ToolStripItemCollection tsic = ((ToolStrip)item).Items;
                tsic.Insert(0, new ToolStripControlHost(boton));       
                return;   
            }   
            for (int i = 0; i < item.Controls.Count; i++)   
            {      
                testc(item.Controls[i]);  
            }   
        }
    }
}

You have to add the button directly in the constructor of the class and you can configure the button in your designer.

Here's a pic of the result, not perfect, but enough to go(safe link I swear, but I can't post my own pics, don't have enough reputation).

http://prntscr.com/5lfssj

If you look carefully in the code of the class, you'd see more or less how it works and you could make your changes and make it possible to establish it in other site of the toolbar.

Thank you so much for helping me in the past, I hope this helps lots of people!

Generally you are suppose to create your own toolbar if you want to modify it. Your solution for removing buttons will probably work if that is all you need to do, but if you want to add your own you should probably just bite the bullet and build a replacement.

You may modify reportviewer controls by CustomizeReportToolStrip method. this example remove Page Setup Button, Page Layout Button in WinForm

public CustOrderReportForm() {
  InitializeComponent();
  CustomizeReport(this.reportViewer1);
}


private void CustomizeReport(Control reportControl, int recurCount = 0) {
  Console.WriteLine("".PadLeft(recurCount + 1, '.') + reportControl.GetType() + ":" + reportControl.Name);
  if (reportControl is Button) {
    CustomizeReportButton((Button)reportControl, recurCount);
  }
  else if (reportControl is ToolStrip) {
    CustomizeReportToolStrip((ToolStrip)reportControl, recurCount);
  }
  foreach (Control childControl in reportControl.Controls) {
    CustomizeReport(childControl, recurCount + 1);
  }
}

//-------------------------------------------------------------


void CustomizeReportToolStrip(ToolStrip c, int recurCount) {
  List<ToolStripItem> customized = new List<ToolStripItem>();
  foreach (ToolStripItem i in c.Items) {
    if (CustomizeReportToolStripItem(i, recurCount + 1)) {
      customized.Add(i);
    }
  }
  foreach (var i in customized) c.Items.Remove(i);
}

//-------------------------------------------------------------

void CustomizeReportButton(Button button, int recurCount) {
}

//-------------------------------------------------------------

bool CustomizeReportToolStripItem(ToolStripItem i, int recurCount) {
  Console.WriteLine("".PadLeft(recurCount + 1, '.') + i.GetType() + ":" + i.Name);
  if (i.Name == "pageSetup") {
    return true;
  }
  else if (i.Name == "printPreview") {
    return true;
  }
  return false; ;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top