Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy way to write Crystal Reports with coded DataSets

Reflection

Easy way to write Crystal Reports with coded DataSets

by  RiverGuy  Posted    (Edited  )
The way I prefer to set up my datasources in the .Net environment for Crystal Reports is via way of my dataSets.

Now if you have dataSet objects in the designer, you can set your report's source to use a dataSet that you have create.

But what if you have all of your dataSets created through your code itself?

The method I like to use is to write the XML schema to a file and then use the report designer, with the .XSD file as the data source.

After you have envoked the fill method of your dataAdapter to fill your dataSets, such as:

dataAdapter1.Fill(dataSet1);

You can then write the schema to an .XSD File, such as:

dataSet1.WriteXmlSchema("Filename.xsd");

Now, during the creation of your report, choose Other Sources for the data source, and ADO.Net (XML). Browse to find your .XSD file. This will give you all of the tables, columns, and data types that your data set contains. You can go about creating your report as you would like.

To populate the report with actual data, here is what you do:

1. Run your program, being sure to envoke the command to write the XSD file.
2. Create a Crystal Report viewer.
3. Create an instance of your report class:
yourCrystalReport cr = new yourCrystalReport();
4. Set the data source for your report instance:
cr.SetDataSource(dataSet1);


That's all there is too it. There are other ways of course. You could probably get away with designing the report from another type of datsource, such as ODBC or Jet, or ADO or whatever, and then changing the dataSource. I haven't tried any of the latter methods, but the XML schema method works well for me. An advantage would be that the schema would remain static, and reusable, and you would not have to go through a larger process of selecting say a SQL Server, and a database, then the tables, etc.

By creating the instance of the report, instead of assigning the actual report class, or .rpt file to the report source of the viewer, you can manipulate the report. An example would be to change the text of formula fields.

This is accomplished by:
cr.DataDefinition.FormulaFields["FieldName"].Text = " CrystalSyntaxHere".

Substituting the name of your report instance for 'cr'.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top