代码之家  ›  专栏  ›  技术社区  ›  Michael Zuschlag

ReportViewer-修改工具栏?

  •  7
  • Michael Zuschlag  · 技术社区  · 18 年前

    对于如何修改ReportViewer工具栏WinForms版本的工具栏,有人有好的想法吗?

    比如,我必须删除导出到excel,并且是这样做的:

      // 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);
        }
      }
    

    如果你问我的话,有点诡计多端。。 要删除工具栏按钮,一种可能的方法是迭代ReportViewer中的控件数组,并更改按钮隐藏的Visible属性,但它会一直重置,因此这不是一种好方法。。

    顺便说一句,MS什么时候会有新版本?

    8 回复  |  直到 18 年前
        1
  •  8
  •   koSmo koSmo    17 年前

    是的。你可以用一种巧妙的方式来做。 我的任务是为缩放报告添加更多比例因子。我是这样做的:

        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;
                }
            }
        }
    
        2
  •  7
  •   Jason Plank Maksim Kondratyuk    15 年前

    从ReportViewer控件获取工具栏:

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

    添加新项目:

    toolStrip.Items.Add(...)
    
        3
  •  4
  •   Biri    18 年前

    例如 ShowBackButton , ShowExportButton , ShowFindControls help ,都以“Show”开头。

    但是你是对的,你不能添加新的按钮。为此,您必须创建自己的工具栏。

    新版本是什么意思?已经有一个 2008 SP1 它的版本。

        4
  •  3
  •   Adrian Grigore    17 年前

    另一种方法是在运行时通过javascript操纵生成的HTML。它不是很优雅,但它确实可以让您完全控制生成的HTML。

        5
  •  2
  •   Joseph    11 年前

    对于VS2013 web ReportViewer V11(指示为rv),下面的代码添加了一个按钮。

    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;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    
        6
  •  1
  •   alex_vkcr    11 年前

    您不需要使用ReportViewer类,而是需要创建一个新类,在我的例子中,我将其命名为ReportViewerPlus,如下所示:

    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]);  
                }   
            }
        }
    }
    

    这里有一张照片的结果,不完美,但足够去(安全链接,我发誓,但我不能张贴我自己的照片,没有足够的声誉)。

    http://prntscr.com/5lfssj

    非常感谢你过去对我的帮助,我希望这能帮助很多人!

        7
  •  0
  •   palehorse    18 年前

    如果您想修改工具栏,通常需要创建自己的工具栏。如果您只需要这样做,那么您的移除按钮的解决方案可能会起作用,但是如果您想添加自己的按钮,您可能应该咬紧牙关,构建一个替换按钮。

        8
  •  0
  •   lison    11 年前

    您可以通过CustomizeReportToolStrip方法修改reportviewer控件。 此示例删除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; ;
    }
    
    推荐文章