Demonstrate Adding a Database Field to the Report
Finally, we get to see how everything works by demonstating the code which creates all the necessary objects and calls the AddDatabaseField() method to put the object on the report. This code uses the Form_Load() event so that report is built immediately when the form opens. Of course, you can copy and paste this code into the appropriate method for your application.
Listing 20-9. Demonstrating adding a database field to the report.
[VB.NET]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Declare both the reporting objects
Dim myReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument = Nothing
Dim rcd As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument = Nothing
OpenReportClientDocument("C:\CrystalReport1.rpt", myReportDocument, rcd)
'Create the fontcolor object
Dim FontColor As CrystalDecisions.ReportAppServer.ReportDefModel.FontColor
FontColor = NewFontColor(10, "Times New Roman", True, False)
'Get a reference to the section where the object will be placed
Dim mySection As CrystalDecisions.ReportAppServer.ReportDefModel.Section
mySection = GetSection("DetailArea1", 0, rcd)
'Add the object to the report
AddDatabaseField("Orders", "Ship Via", 0, 1000, 221, 1000, mySection, FontColor, rcd)
CrystalReportViewer1.ReportSource = myReportDocument
End Sub
[C#]
private void Form1_Load(object sender, EventArgs e)
{
// Declare both of the reporting objects
CrystalDecisions.CrystalReports.Engine.ReportDocument myReportDocument = null;
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rcd = null;
//Load the report into memory
OpenReportClientDocument(@"C:\CrystalReport1.rpt", ref myReportDocument, ref rcd);
//Create the fontcolor object
CrystalDecisions.ReportAppServer.ReportDefModel.FontColor FontColor;
FontColor = NewFontColor(10, "Times New Roman", true, false);
//Get a reference to the section where the report object will be placed
CrystalDecisions.ReportAppServer.ReportDefModel.Section mySection;
mySection = GetSection("DetailArea1", 0, rcd);
//Create the report object and place it on the report
AddDatabaseField("Orders", "Ship Via", 0, 1000, 221, 1000, mySection, FontColor, rcd);
//Preview the report in the viewer
crystalReportViewer1.ReportSource = myReportDocument;
}