当前位置:  开发笔记 > 编程语言 > 正文

ReportViewer - 修改工具栏?

如何解决《ReportViewer-修改工具栏?》经验,为你挑选了2个好方法。

有没有人对如何修改ReportViewer工具栏的WinForms版本的工具栏有很好的想法?也就是说,我想删除一些按钮和varius,但看起来解决方案是创建一个全新的工具栏,而不是修改那里的那个.

就像,我不得不删除导出到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中的Control数组并更改按钮的Visible属性以隐藏,但它会一直重置,所以它不是好办法..

什么时候MS有一个新版本顺便说一下?



1> 小智..:

叶氏.你可以用一点点棘手的方式做到这一点.我有一项任务是添加更多比例因子来缩放报告.我是这样做的:

    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> 小智..:

从ReportViewer控件获取工具栏:

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

添加新项目:

toolStrip.Items.Add(...)

推荐阅读
爱唱歌的郭少文_
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有