Click here to Skip to main content
15,893,486 members
Articles / Web Development / ASP.NET
Tip/Trick

How to Create Custom Paging for a Crystal Report Viewer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
11 Jan 2019CPOL2 min read 11.9K   3   1
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

Image 1

XML
<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.NET
<asp:HiddenField runat="server" ID="hid_current_page" Value="1" />

Step 3: Now We Need Four Buttons for the Navigation

Image 2

ASP.NET
<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

C#
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

C#
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.

C#
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

C#
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

C#
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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncan you send the png files Pin
uniconahd2-Feb-20 20:02
uniconahd2-Feb-20 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.