Click here to Skip to main content
Click here to Skip to main content

Dynamic Crystal Reports Viewing

By , 22 Apr 2004
 

Sample Image

Introduction

The code shows how anyone can make a dynamic Crystal Reports application that can easily be distributed to your users.

Background

So many times at google.com, I have noticed that Crystal Reporting is a problem for many users (especially myself). I finally found a quick and easy recipe to handle any type of reporting requirements. In other words, I think this solution works fine by itself and equally fine when integrated into an existing application.

Using the code

Slap the code into your application and create a rpt subdirectory as part of the setup project (under the application folder), then fill with Crystal Reports that have been created using an ODBC connection (do not click Save Data in the Crystal Report). After this, you are "cooking with gas" (ready to go...).

Set the following properties:

Form Properties
===============
Starting Position = Maximized, Center of Screen

Report Viewer Properties
========================
Anchor (Top,Bottom, Left, Right)
Dock (Bottom)

The key parts of this easy application centers on the following routines:

private void Form1_Load(object sender, System.EventArgs e)
{
    //Populate the Combo Box with the report names
    cBxRptList_Fill();
}

private void cBxRptList_Fill()
{
    //Create a temp table to hold the reports description and path
    DataTable cBxContents = new DataTable ();
    cBxContents.Columns.Add ("Description", System.Type.GetType("System.String"));
    cBxContents.Columns.Add ("Path",System.Type.GetType("System.String"));

    //Create the first row of the temp table to tell users what to do
    string[] FirstRow = {" -- select a report to view --", "-1"};
    cBxContents.Rows.Add (FirstRow);

    //Pickup all the crystal reports in the rpts subdirectory of the program
    string[] fileList =  Directory.GetFiles (Directory.GetCurrentDirectory() 
                                                      + @"\rpts", "*.rpt");
    foreach (string item in fileList)
    {
        //add all the friendly report names into the temp table
        int startPt = item.ToString().LastIndexOf(@"\rpts");
        string[] rowData = {item.Substring(startPt + @"\rpts".Length + 1,
        item.ToString ().Length - (startPt + @"\rpts".Length + 1)),item};
        cBxContents.Rows.Add (rowData);
    }

    //assign the temp table to the combo box
    cBxRptList.DataSource = cBxContents;
    cBxRptList.DisplayMember = "Description";
    cBxRptList.ValueMember = "Path";
}

private void cBxRptList_SelectedIndexChanged(object sender, System.EventArgs e)
{
    //identify if the first combo box item is not selected
    switch (cBxRptList.SelectedIndex > 0)
    {
        //if another line in the combo box is selected then view the report 
        case true:
            cRVMain.ReportSource = cBxRptList.SelectedValue ;
            cRVMain.Zoom (25);
            break;

        //if the first line in the combo box is selected then clear the report
        case false:
            cRVMain.ReportSource = null;
            break;
    }

The full listing of code includes the IDE generated code that appears after one drops a ComboBox and a Crystal Report Viewer onto a basic form. Note that I renamed my ComboBox -> cBxRptList and the Crystal Report Viewer -> cRVMain. I also created a DataTable to hold the description and the path information for the reports, called cBxContents.

Points of Interest

When one reopens the form in design mode, the viewer seems to be pulled out of position (reset), I am not sure why (seems like a bug to me).

I learned that rather than putting lots of auxiliary stuff (like miscellaneous folders) into the executable, that one should put all the extras into the setup deliverable. For instance, I put the actual reports into the setup file by adding them under the rpts folder (which is under the Application folder).

History

  • 4/21/04 - Changed the routines to use a friendly list of names rather than just assigning the entire path names to the list of reports.

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

About the Author

Tommie Carter
Web Developer
United States United States
Member
Hi all,
 
I am a long-time coder. Since 8th grade with Apple and Commodore Pet! Now, I'm a computer consultant and software developer in new york, ny.
 

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Question"The report has no tables"memberIbrahim Islam6 Jan '13 - 23:24 
Since, No datasource has been shown on the .rpt file should not there been the "The report has no table" error be encountered?
Questiondynamic reportmemberpritesh.borse9 Aug '10 - 19:02 
how to add
header run time, in cristal report
GeneralPublish Crystal Report to the web using Microsoft ServicesmemberAdamster20210 Jun '09 - 13:22 
HI I am in need to start using MS Services on SQL Server 2005 to publish Crystal Reports to the web. Is this being done. Perhaps someone can outline what has to be done on the MS side?
GeneralDyanmic Crystal Reportmemberadeelidrees11 May '09 - 21:47 
I am newbie to crystal reports and I want to create the date wise stock
 
Detail is below:
 
I have three fields
 
-- date
-- purchased quantity
-- sold quantity
 
the above fields are showing the required data but now I want to create field
 
"remaining quantity"
 
The formula for this filed should be like this
 
for the first value
purchased_quantity(0)-sold_quantity(0)
for others it should be
 
remaining_quantity(0)+purchased_quantity(1)-sold_quantity(1)
remaining_quantity(1)+purchased_quantity(2)-sold_quantity(2)
remaining quantity(2)+purchased quantity(3)-sold_quantity(3)
.............
 

example is here below:
 
Date Purchase_Qty Sold_Qty Rem_Qty
8-05-09 200 100 100
9-05-09 100 50 150
10-05-09 50 10 190
 

Please help me. I make the sense but cannot know about formula creation at runtime.
GeneralClearing the ViewmemberJose Raul Funez27 Feb '07 - 7:09 
How do you clear the view of the actual report, that is, when you're sending parameters and you want to view it again with new para meters but before you do you want to clear the view, like when you first load the form D'Oh! | :doh:
 
Raúl Fúnez
AnswerRe: Clearing the ViewmemberTommie Carter5 Jul '07 - 8:12 
Jose Raul Funez wrote:
How do you clear the view of the actual report, that is, when you're sending parameters and you want to view it again with new para meters but before you do you want to clear the view, like when you first load the form

 
Raul,
 
You can clear the report at any time by calling:
 
cRVMain.ReportSource = null;
 
Hope that helps Wink | ;)
 
Meditate daily on the vast and the empty. Release attachments.

Generalgenerate charts by codingmemberMember #386614626 Feb '07 - 5:30 
how to generate charts by coding
 
janee
AnswerRe: generate charts by codingmemberTommie Carter5 Jul '07 - 8:49 
I will have to investigate this question a bit. My basic premise in the original article was simply dynamically binding a number of pre-configured reports to a viewer. This allowed a significant number of users to view the reports without needing to have a full version of CR installed.
 
The original premise was that a report person would then generate and distribute the report. Having said this charts can be pre-configured and bound to data connections just like any other report.
 
I will explore how much code control of CR is allowed and post back my findings.
 
Thanks for the good question.
Smile | :)
 
Meditate daily on the vast and the empty. Release attachments.

http://tommiecarter.blogspot.com


Generaljadynamic crystal reportsmemberMember #386614626 Feb '07 - 5:20 
how to generate crystal reports dynamically by accessing data from database
 
janee
Generaldynamic crystal reportsmemberMember #386614626 Feb '07 - 5:19 
how to generate crystal reports dynamically by accessing data from database

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 23 Apr 2004
Article Copyright 2004 by Tommie Carter
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid