Click here to Skip to main content
13,459,040 members
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 22 Apr 2004

Dynamic Crystal Reports Viewing

, 22 Apr 2004
Rate this:
Please Sign up or sign in to vote.
An article on simplified Crystal Reporting using C#.NET.

Sample Image


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


So many times at, 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

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);

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

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).


  • 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.


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

You may also be interested in...

Comments and Discussions

Question"The report has no tables" Pin
Ibrahim Islam6-Jan-13 23:24
memberIbrahim Islam6-Jan-13 23:24 
Questiondynamic report Pin
pritesh.borse9-Aug-10 19:02
memberpritesh.borse9-Aug-10 19:02 
GeneralPublish Crystal Report to the web using Microsoft Services Pin
Adamster20210-Jun-09 13:22
memberAdamster20210-Jun-09 13:22 
GeneralDyanmic Crystal Report Pin
adeelidrees11-May-09 21:47
memberadeelidrees11-May-09 21:47 
GeneralClearing the View Pin
Jose Raul Funez27-Feb-07 7:09
memberJose Raul Funez27-Feb-07 7:09 
AnswerRe: Clearing the View Pin
Tommie Carter5-Jul-07 8:12
memberTommie Carter5-Jul-07 8:12 
Generalgenerate charts by coding Pin
Member #386614626-Feb-07 5:30
memberMember #386614626-Feb-07 5:30 
AnswerRe: generate charts by coding Pin
Tommie Carter5-Jul-07 8:49
memberTommie Carter5-Jul-07 8:49 
Generaljadynamic crystal reports Pin
Member #386614626-Feb-07 5:20
memberMember #386614626-Feb-07 5:20 
Generaldynamic crystal reports Pin
Member #386614626-Feb-07 5:19
memberMember #386614626-Feb-07 5:19 
AnswerRe: dynamic crystal reports Pin
Tommie Carter5-Jul-07 8:34
memberTommie Carter5-Jul-07 8:34 
In the example provided the database connections are maintained in the report file. The data is dynamically populated in that if the underlying data changes then the report contains the new data.

There are many other methods to dynamically adopt changed data and implement it in Crystal Reports. Please provide a little more information:

Language (C#/VB)
Environment (Web/Windows)
Crystal Reports Version

Meditate daily on the vast and the empty. Release attachments.

QuestionHow to generate dynamic crystal reports - .NET Pin
tinh cau4-Jan-07 17:02
membertinh cau4-Jan-07 17:02 
QuestionHow to create a Dynamic Crystal Report using C# and MSSQL Pin
tinh cau4-Jan-07 17:01
membertinh cau4-Jan-07 17:01 
AnswerRe: How to create a Dynamic Crystal Report using C# and MSSQL Pin
Tommie Carter5-Jul-07 8:30
memberTommie Carter5-Jul-07 8:30 
GeneralMake Application Pin
Aunalisiraj24-May-05 20:39
memberAunalisiraj24-May-05 20:39 
AnswerRe: Make Application Pin
Tommie Carter5-Jul-07 8:27
memberTommie Carter5-Jul-07 8:27 
GeneralCrystal Report 9/10 and .NET Remoting Pin
a_dipendra28-Jan-05 4:36
membera_dipendra28-Jan-05 4:36 
AnswerRe: Crystal Report 9/10 and .NET Remoting Pin
Tommie Carter5-Jul-07 8:20
memberTommie Carter5-Jul-07 8:20 

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.

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