views:

2519

answers:

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?

A: 

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
+1  A: 

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
A: 

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

I guess I have been living inside something.. arf, did not see those properties until now, and thanks for the link for SP1, totally missed that.

neslekkiM
+1  A: 

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;
            }
        }
    }
A: 

This article can be useful to customize toolbar.

frameworkninja
A: 

Get the toolbar from ReportViewer control:

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

Add new items:

toolStrip.Items.Add(...)

Chris

Chris
+1  A: 

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
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
@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
@Adrian 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
@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

related questions