Click here to Skip to main content
13,899,494 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.4K views
3 bookmarked
Posted 13 Aug 2018
Licenced CPOL

How to Create Custom Paging for a Crystal Report Viewer

, 11 Jan 2019
Rate this:
Please Sign up or sign in to vote.
This article will provide you the alternative way to navigate page in a crystal report viewer, as default paging looks broken

Introduction

I found that page navigation of Crystal report viewer is kind of broken. There are different solutions to this problem on the internet but sometimes, it's not possible to use certain solutions because of their limitation or compatibilities. So I will show you how to create custom paging for Crystal report viewer.

Background

It's going to help you if you have any kind of knowledge of Crystal report viewer and C#.

Using the Code

I would guess that viewer knows how to set up a Crystal report viewer in ASP.NET.

Step 1: First Define a Crystal Report Viewer in Your Page Like This

<cr:crystalreportviewer autodatabind="true" 

bestfitpage="False" id="CrystalReportViewer1" 

runat="server">DisplayToolbar="true" 
ToolPanelView="None" SeparatePages="true" 
Width="100%" HasPageNavigationButtons="False" />

Make sure to keep DisplayToolbar property as true and HasPageNavigationButtons as false. We want to show toolbar but to hide the default page index change buttons.

Step 2: Put a Hidden Field Just Below the Crystal Report Viewer

<asp:HiddenField runat="server" ID="hid_current_page" Value="1" />

Step 3: Now We Need Four Buttons for the Navigation

<table>

<tr>
  <td>
  <asp:ImageButton ID="btn_first" runat="server" 

ImageUrl="~/Images/First.png" OnClick="btn_first_Click" />
  </td>
  <td>
  <asp:ImageButton ID="btn_prev" runat="server" 

ImageUrl="~/Images/previous.png" OnClick="btn_prev_Click" />

</td>
<td>
   <asp:ImageButton ID="btn_next" runat="server" 

ImageUrl="~/Images/next.png" OnClick="btn_next_Click" /></td>
   <td>
   <asp:ImageButton ID="btn_last" runat="server" 

ImageUrl="~/Images/last.png" OnClick="btn_last_Click" /></td>
   </tr>

</table>

So let me tell you how things will work. We have taken a hidden field to store the current page number of the Crystal report viewer. By default, its value will be 1, which will indicate the first page the Crystal report viewer. Following are some Crystal report predefined functions that will help us to set the page of the report.

  1. ShowFirstPage() - This will set the first page of Crystal report viewer
  2. RefreshReport() - Use to refresh the value of Crystal report
  3. ShowNthPage(n) - To set nth page of Crystal report viewer

Following is the code for the navigation button click:

1. First Page Button is Clicked Event

protected void btn_first_Click(object sender, ImageClickEventArgs e)
    {
        CrystalReportViewer1.ShowFirstPage(); // We are sure that user want to see first page
        CrystalReportViewer1.RefreshReport(); 
        hid_current_page.Value = "1"; // we will reset hidden page value to 1
    }

2. Previous Page Button Clicked Event

protected void btn_prev_Click(object sender, ImageClickEventArgs e)
    {
        // Extract on which page currently report is set to.
        int current_page = Convert.ToInt32(hid_current_page.Value);

        //To check if Previous button is clicked when current page No. is set to first page 
        if ((current_page - 1) < 1)   
        {
            //If so, set crystal report to first page.
            hid_current_page.Value = "1";
            CrystalReportViewer1.ShowNthPage(1);
        }
        else
        {
            // If not set to one previous page according to current page number.
            hid_current_page.Value = Convert.ToString(current_page - 1);
            CrystalReportViewer1.ShowNthPage(current_page - 1);
        }
        // Make sure to refresh the report viewer
        CrystalReportViewer1.RefreshReport();
    }

3. When Next Page Button is Clicked

But before setting the next page, we need to find the last page of the Crystal report.

private int getLastPageNumber(CrystalReportViewer crview)
    {
        crview.ShowLastPage();  //To navigate to the last page no. 
        CrystalDecisions.Web.ViewInfo vi = crview.ViewInfo; //Get info of the navigated page 
        return vi.PageNumber;
    }

3.1 Next Page Button click event

protected void btn_next_Click(object sender, ImageClickEventArgs e)
    {
        int current_page = Convert.ToInt32(hid_current_page.Value);
        int last_page = getLastPageNumber(CrystalReportViewer1);
        //To check if Next button is clicked when current page No. is set to last page.
        if ((current_page + 1) > last_page)  
        {
            hid_current_page.Value = Convert.ToString(last_page);
            CrystalReportViewer1.ShowNthPage(last_page);
        }
        else
        {
            hid_current_page.Value = Convert.ToString(current_page + 1);
            CrystalReportViewer1.ShowNthPage(current_page + 1);
        }
        CrystalReportViewer1.RefreshReport();
    }

4. Last Page Button Clicked Event

protected void btn_last_Click(object sender, ImageClickEventArgs e)
    {
        int last_page = getLastPageNumber(CrystalReportViewer1);
        hid_current_page.Value = Convert.ToString(last_page);
        CrystalReportViewer1.ShowNthPage(last_page);         //To navigate to the last page no.
        CrystalReportViewer1.RefreshReport();
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

akshay_zz
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 12 Jan 2019
Article Copyright 2018 by akshay_zz
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid