
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)
{
cBxRptList_Fill();
}
private void cBxRptList_Fill()
{
DataTable cBxContents = new DataTable ();
cBxContents.Columns.Add ("Description", System.Type.GetType("System.String"));
cBxContents.Columns.Add ("Path",System.Type.GetType("System.String"));
string[] FirstRow = {" -- select a report to view --", "-1"};
cBxContents.Rows.Add (FirstRow);
string[] fileList = Directory.GetFiles (Directory.GetCurrentDirectory()
+ @"\rpts", "*.rpt");
foreach (string item in fileList)
{
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);
}
cBxRptList.DataSource = cBxContents;
cBxRptList.DisplayMember = "Description";
cBxRptList.ValueMember = "Path";
}
private void cBxRptList_SelectedIndexChanged(object sender, System.EventArgs e)
{
switch (cBxRptList.SelectedIndex > 0)
{
case true:
cRVMain.ReportSource = cBxRptList.SelectedValue ;
cRVMain.Zoom (25);
break;
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.