7

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?

neslekkiM
  • 693
  • 1
  • 8
  • 19

8 Answers8

8

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;
            }
        }
    }
  • thank you! you just solved a problem I was having due to excessive zoom factors! (500% on a particular report was causing an exception from deep in the bowels of win32...) – matao Dec 01 '11 at 04:44
7

Get the toolbar from ReportViewer control:

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

Add new items:

toolStrip.Items.Add(...)
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Chris
  • 71
  • 1
  • 1
4

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.

Biri
  • 7,101
  • 7
  • 38
  • 52
3

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.

Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210
  • I realize this is an old post, but could you elaborate on how that would be done? It sounds like that could solve some of the issues I have wrestling with ReportViewer's seemingly random and unsemantic HTML. – firedrawndagger Aug 20 '10 at 14:43
  • 2
    @firedrawndagger: There's nothing fancy about it. I'm using a jQuery $(document).ready() handler for this and manipulate the reportviewer html bar from there. The new ReportViewer component in .NET 4 is by default based on ajax (no Iframe), which makes this even easier. But even if your report is inside an Iframe, there are still ways to access that via jquery. Sorry that I can't post any code (it would be too much hassle to present it in an understandable manner), but I can assure you it works and it's not really that difficult. – Adrian Grigore Aug 20 '10 at 18:17
  • thanks, unfortunately I'm still stuck with .NET3.5 and the iframe for now but I guess I'll mess around with JQuery and see what happens. – firedrawndagger Aug 20 '10 at 18:30
  • @firedrawndagger: My previous version of the ReportViewer mod script was based on the 3.5 ReportViewer and jQuery as well and took about 2-3 hours to code, so it definitely is feasible. Good luck! – Adrian Grigore Aug 20 '10 at 22:59
  • I Have done this to add a custom print button, which works on any browser :) – Guillermo Gutiérrez Jul 04 '13 at 22:56
  • @guillegr123: Interesting! Do you have perhaps a blog posting related to this? I'd like to know more about the details. – Adrian Grigore Jul 06 '13 at 11:36
  • I just have blog post of a first version, adding the button outside the report viewer, but it is in spanish, hehehe: http://itsouvenirs.wordpress.com/2013/05/05/imprimir-reporte-de-reportviewer-desde-otro-navegador-web-que-no-sea-ie/. I will try to post the second version between today and tomorrow, which actually replaces the print button of the report viewer. – Guillermo Gutiérrez Jul 06 '13 at 13:47
  • Sorry, it took some time, but I have finally written the blog post about adding a custom print button to the report viewer (in spanish): http://itsouvenirs.wordpress.com/2013/07/15/crear-boton-de-impresion-en-reportviewer-de-asp-net-webforms/ – Guillermo Gutiérrez Jul 16 '13 at 14:04
2

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;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
Joseph
  • 21
  • 2
1

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!

alex_vkcr
  • 64
  • 5
  • Hi @Alex this is a useful solution, but in existing toolbar printer button will be enabled only after report is loaded. How can i enable button after report is enabled, which event is used for this? – Jigar Parekh Aug 29 '16 at 14:21
0

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.

palehorse
  • 26,407
  • 4
  • 40
  • 48
0

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; ;
}
lison
  • 189
  • 1
  • 6