Disable ReportViewer Page Navigation On Mouse Wheel

Question

I have a report that has a lot of data formatted to be printer friendly. On average there are 16 to 24 pages of data which my users can page through. I decided to make a windows forms ReportViewer (imported into WPF) and it works very well except for one thing: I cannot find how to disable the automatic page advance on scrolling.

I have looked through my rdlc report properties and the ReportViewer both and don't see any properties. I cannot find any information online either for it. is this not a feature that can be disabled or am I just not seeing it?


Show source
| c#   | wpf   | winforms   | reportviewer   | rdlc   2016-12-21 18:12 1 Answers

Answers ( 1 )

  1. 2016-12-22 06:12

    ReportViewer performs page navigation on mouse wheel by overriding OnMouseWheel method of an internal control named ReportPanel, so we can not override it, because it's internal.

    ReportViewer control has a PageNavigation event which we can use it to cancel navigation. But we should distinguish if the event is raised by toolbar buttons or by mouse wheel. To do so, we handles Click event of toolbar buttons in code (and remove default event handler) and set a flag which will be used later to determine the navigation event is raised by toolbar.

    I wrote a method which encapsulates the logic and makes disabling wheel navigation easy. To do so, it's enough to call the method this way:

    DisableWheelNavigation(reportViewer1);
    

    And here is the implementation of the method:

    public void DisableWheelNavigation(ReportViewer r) {
        bool fromToolbar = false;
        r.PageNavigation += (obj, ea) => {
            if (fromToolbar) fromToolbar = false;
            else ea.Cancel = true;
        };
        var buttons = new string[] { "firstPage", "previousPage", "nextPage", "lastPage" };
        var toolstrip = r.Controls.Find("toolStrip1", true).OfType<ToolStrip>().First();
        toolstrip.Items.OfType<ToolStripButton>()
            .Where(button => buttons.Contains(button.Name)).ToList().ForEach(item => {
                var clickEvent = item.GetType().GetEvent("Click");
                var removeMethod = clickEvent.GetRemoveMethod();
                var d = Delegate.CreateDelegate(clickEvent.EventHandlerType,
                    toolstrip.Parent, "OnPageNavButtonClick");
                removeMethod.Invoke(item, new object[] { d });
                item.Click += (obj, ev) => {
                    var onPageNavigation = toolstrip.Parent.GetType()
                        .GetMethod("OnPageNavigation",
                    BindingFlags.NonPublic | BindingFlags.Instance);
                    Action<int> OnPageNavigation = i => {
                        fromToolbar = true;
                        onPageNavigation.Invoke(toolstrip.Parent, new object[] { i });
                    };
                    if (item.Name == "firstPage") OnPageNavigation(1);
                    else if (item.Name == "previousPage") OnPageNavigation(r.CurrentPage - 1);
                    else if (item.Name == "nextPage") OnPageNavigation(r.CurrentPage + 1);
                    else if (item.Name == "lastPage") {
                        PageCountMode mode;
                        int totalPages = r.GetTotalPages(out mode);
                        if (mode != PageCountMode.Actual) OnPageNavigation(0x7fffffff);
                        else OnPageNavigation(totalPages);
                    }
                };
            });
    }
    
◀ Go back