Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Create Custom Paging for a Crystal Report Viewer

0.00/5 (No votes)
11 Jan 2019 4  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here