Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives


How to: Tweak the Crystal Reports Viewer control

Novita Wegbrands, a colleague of mine, needed to hide the tabs that are usually shown in the Crystal reports viewer. Either that, or change the default "MainReport" description. She found some code here on forums.belution.com/en/crystal that suited her needs. She sent the code to me (thanks Novita!) and I changed them a little so they would fit my library of Crystal Helper classes. So here's what we have now.

Show / Hide the status bar

By default, the Crystal Reports viewer shows a status bar at the bottom to indicate the current page, the total number of pages and the current zoom factor. But maybe you'd like to hide it. No problem, the following code will do just that:

public void ViewerStatusBar(CrystalReportViewer viewer, bool visible)
{
    foreach (Control control in viewer.Controls)
    {
        if (control.GetType().Name.ToString() == "StatusBar")
        {
            control.Visible = visible;
        }
    }
}
 

Show / Hide the tabs

The Crystal Reports viewer will also display tabs for the report. By default there is always one. But when it not possible to perform a drill-down in a report then this tab is virtually useless. Using the following code, you can hide those tabs:

public void ViewerTabs(CrystalReportViewer viewer, bool visible)
{
    foreach (Control control in viewer.Controls)
    {
        if (control is PageView)
        {
            TabControl tab = (TabControl)((PageView)control).Controls[0];
            if (!visible)
            {
                tab.ItemSize = new Size(0, 1);
                tab.SizeMode = TabSizeMode.Fixed;
                tab.Appearance = TabAppearance.Buttons;
            }
            else
            {
                tab.ItemSize = new Size(67, 18);
                tab.SizeMode = TabSizeMode.Normal;
                tab.Appearance = TabAppearance.Normal;
            }
        }
    }
}
 
The ItemSize that is created on the tab control is a default size. Because the SizeMode property is set to Normal, Crystal Reports will automatically scale the tab to fit the name.

Change the name of the tab

As stated before, the default name for the first tab is usually MainReport. It is possible to change this by using the following code:

Comments

SAK said:

it's good what if i dont want to hide the status bar but to show the correct number of total pages, because it's not showing the correct number instead it shows total pages=1+
regards SAK
# October 24, 2005 1:02 AM

Prema said:

How to hide the Main Reprot Display? I just want to display the report page nothing else.

# July 5, 2007 11:53 AM

Jan Schreuder said:

Use the ViewerTabs method and set the Visible argument to false

# July 6, 2007 10:03 AM

Sandeep said:

How to show total number of pages in crystal report instead of 1+, 2+, 3+ ....

# August 9, 2007 11:55 AM

Jan Schreuder said:

Show where? In the document is easy, it's a special field.

# August 9, 2007 4:05 PM

Anish said:

Gr8 works fine!!

# June 5, 2008 8:15 AM

thecardinal said:

it say that the cast of toolstrip to the pageview and i get an error?

# June 17, 2008 4:01 AM

Jan Schreuder said:

You could try and download the code for my Crystal Helper class here: www.codeproject.com/.../CrystalHelper.aspx. The helper class contains methods that do what I describe in this post. There's also a demo application in there that shows how the methods work.

# June 18, 2008 9:02 AM

Sandeep Targe(Rahata) said:

I cant get the event ::ViewerStatusBar & ViewerTabs, do we have to import any other classes?? Can any one help me?

# July 21, 2008 2:01 PM

Jan Schreuder said:

ViewerStatusBar and ViewerTabs are methods, not events. So when you want to use these methods to disable the tabs and/or status bar then you simply call them when the control will be displayed.

# July 22, 2008 11:10 AM

Eddie said:

Thank you for posting this. It works very well. The VB.NET 2008 version is:

   Private Sub HideCrystalViewerTab()

       Dim tempcontrol As TabControl = crtPDFView.Controls(0).Controls(0)

       With tempcontrol

           .ItemSize = New Size(0, 1)

           .SizeMode = TabSizeMode.Fixed

           .Appearance = TabAppearance.Buttons

       End With

   End Sub

# August 8, 2008 10:11 AM

Oleg said:

How come I can't find the TabControl type, what namespace do I need? Thanks

# January 23, 2009 10:07 PM

Jan Schreuder said:

depends on which tab control you need, the Windows version or the Crystal version. If you have VS 2005, try resolve to find the namespace.

# January 25, 2009 11:13 PM

Igor said:

Thanks for your work. I've got 1 question: what event i should process to change name of subreport?

# March 12, 2009 11:40 AM

sheraz said:

I have a problem opening the report in crystalreportviewer. whenever i first time open the report it goes to sleep. alomst takes 45 secs - 1 minute or more to open it.

but in the same run if i open the report again it shows like it has everything in it.

is there any way to speed up the first time loading time of the viewer in dot net 2005.

i am using csharp code.

msherazkhan@gmail.com

# April 15, 2009 6:52 PM

Jan Schreuder said:

I think, judging by your comment, that the queries that retrieve the data take long to run. The second time, the data will be in the system cache, so it will be returned to your report faster. Try checking the time it takes to retrieve the data for your report, and see if the queries can be optimized.

If you use DataSet objects, try populating them in a separate thread. That will allow you to load the data while your application is waiting for the user to select the report.

# April 15, 2009 7:28 PM

shahzad said:

Can I hide business object button on top right corner?

# August 24, 2009 10:53 PM

Jan Schreuder said:

Yes you can. But you'll have to find the control to disable yourself. What my code does is walk through the tree of controls in the viewer control and disables the ones I recognize. A similar trick needs to be done for the BO button.

# August 26, 2009 7:31 PM

Hasan said:

Thanks it is works.

# November 18, 2009 7:26 AM

Chris said:

Hi There,

Can I reuse the parameters when the refresh button is clicked? Please advice,

Thank you

# February 9, 2010 1:50 AM

Prajakta said:

I need to display exact no of pages instead of 1+. Can anybody help me?

# February 9, 2010 10:03 AM

Jan Schreuder said:

@Chris: yes that should be possible.

@Prajakta: Problem is that CR only knows the exact number of pages once it has rendered all pages. So the only option is to do a navigate to the last page and then back to the first. Which could be time consuming.

# February 9, 2010 11:58 AM

Shaun721 said:

Where did the code example for Change the name of the tab go?

# June 17, 2010 8:37 PM

Jan Schreuder said:

I honestly don't know. But if you download the code for my Crystal Helper class @ www.codeproject.com/.../CrystalHelper.aspx, then you will find the code in the helper class.

# June 17, 2010 9:22 PM

Subhasish Mukherjee said:

Thank you very much. The tab was annoying me. I used crystal report 9 in vb6.0 and found very handy but these type of few things were making me crazy.

Thanks once again. Any help get me m.subhasish@gmail.com

# June 29, 2010 8:44 AM

vuthatann said:

when i fellow code like this:

Private Sub HideCrystalViewerTab()

      Dim tempcontrol As TabControl = crtPDFView.Controls(0).Controls(0)

      With tempcontrol

          .ItemSize = New Size(0, 1)

          .SizeMode = TabSizeMode.Fixed

          .Appearance = TabAppearance.Buttons

      End With

  End Sub

if appear one message "Index out of rang" in vb.net2008 with sql200.

have any idea, please help me

# July 9, 2010 2:32 AM

Steve said:

You can disable the Business Objects button by...

Viewer.ShowLogo = false;

# January 25, 2011 9:12 PM

Christian said:

Hello there!

When I try the above code, I get an error message (translated to english):

'Cannot convert type "System.Web.UI.Control" to "CrystalDecisions.Windows.Forms.PageView"'

here:

[code]

TabControl tab = (TabControl)((PageView)control).Controls[0];

[/code]

# November 8, 2011 12:30 PM

Jan Schreuder said:

@Christian. That is because the code assumes you are building a winforms app, and you're trying to execute it on a web application

# November 8, 2011 4:06 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 4 and 5 and type the answer here: