Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

15.11 C# Code Listings

C# Code Listings

The C# code listings are equivalent to the VB.NET code listings.

Listing 15-1. Changing the selection formula with the ReportDocument object.
CrystalReport1 MyReport = new CrystalReport1();
MyReport.DataDefinition.RecordSelectionFormula =
"{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)";
crystalReportViewer1.ReportSource = MyReport;
Listing 15-2. Changing the selection formula with the viewer control.
CrystalReport1 MyReport = new CrystalReport1();
crystalReportViewer1.ReportSource = MyReport;
crystalReportViewer1.SelectionFormula =
"{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 02, 20)
Listing 15-3. Mapping the ReportDefinition object model.
private void Form1_Load(object sender, System.EventArgs e)
{
CrystalReport1 MyReport = new CrystalReport1();
MapAreas(MyReport.ReportDefinition.Areas, 0);
}
public void MapAreas(CrystalDecisions.CrystalReports.Engine.Areas Areas, int Indent)
{
foreach(CrystalDecisions.CrystalReports.Engine.Area Area in Areas)
{
MapArea(Area, Indent);
}
}
public void MapSections(CrystalDecisions.CrystalReports.Engine.Sections Sections, int Indent)
{
foreach(CrystalDecisions.CrystalReports.Engine.Section Section in Sections)
{
MapSection(Section, Indent);
}
}
public void MapReportObjects(CrystalDecisions.CrystalReports.Engine.ReportObjects ReportObjects, int Indent)
{
foreach(CrystalDecisions.CrystalReports.Engine.ReportObject ReportObject in ReportObjects)
{
MapReportObject(ReportObject, Indent + 2);
}
}
public void MapArea(CrystalDecisions.CrystalReports.Engine.Area Area, int Indent)
{
Output("Area: " + Area.Name, Indent);
Output("Kind: " + Area.Kind.ToString(), Indent+2);
Output("Suppress: " + Area.AreaFormat.EnableSuppress.ToString(), Indent + 2);
Output("Keep Together: " + Area.AreaFormat.EnableKeepTogether.ToString(), Indent + 2);
foreach(CrystalDecisions.CrystalReports.Engine.Section Section in Area.Sections)
{
MapSection(Section, Indent + 2);
}
}
public void MapSection(CrystalDecisions.CrystalReports.Engine.Section Section, int Indent)
{
//Print the Section properties
Output("Section: " + Section.Name, Indent);
Output("Suppress: " + Section.SectionFormat.EnableSuppress.ToString(), Indent);
Output("Keep Together: " + Section.SectionFormat.EnableKeepTogether.ToString(), Indent);
foreach(CrystalDecisions.CrystalReports.Engine.ReportObject ReportObject in Section.ReportObjects)
{
MapReportObject(ReportObject, Indent + 2);
}
}
public void MapReportObject(CrystalDecisions.CrystalReports.Engine.ReportObject ReportObject, int Indent)
{
switch(ReportObject.Kind)
{
case CrystalDecisions.Shared.ReportObjectKind.TextObject:
{
MapTextObject(((CrystalDecisions.CrystalReports.Engine.TextObject)ReportObject), Indent);
break;
}
case CrystalDecisions.Shared.ReportObjectKind.FieldObject:
{
MapFieldObject(((CrystalDecisions.CrystalReports.Engine.FieldObject)ReportObject), Indent);
break;
}
}
}
public void MapTextObject(CrystalDecisions.CrystalReports.Engine.TextObject TextObject, int Indent)
{
Output("Name: " + TextObject.Name, Indent);
Output("Kind: " + TextObject.Kind.ToString(), Indent + 2);
Output("Text: " + TextObject.Text, Indent + 2);
}
public void MapFieldObject(CrystalDecisions.CrystalReports.Engine.FieldObject FieldObject, int Indent)
{
Output("Name: " + FieldObject.Name, Indent);
Output("Kind: " + FieldObject.Kind.ToString(), Indent + 2);
Output("DataSource.Name: " + FieldObject.DataSource.Name, Indent + 2);
Output("DataSource.Formula: " + FieldObject.DataSource.FormulaName, Indent + 2);
}
public void Output(string Line, int Indent)
{
lstMapping.Items.Add(new string(' ',Indent) + Line);
}
Listing 15-4. Get a reference to the ReportFooter section.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.Section MySection;
MySection = MyReport.ReportDefinition.Sections["ReportFooter"];
Listing 15-5. Get a reference to the report object State using the ReportObject data type.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.ReportObject MyObject;
MyObject = MyReport.ReportDefinition.ReportObjects["State"];
Listing 15-6. Get a reference to the State object using the object’s specific data type.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.FieldObject MyField;
MyField = (CrystalDecisions.CrystalReports.Engine.FieldObject)
MyReport.ReportDefinition.ReportObjects["State"];
Listing 15-7. Modify properties of the ReportFooter section.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.Section MySection;
MySection = MyReport.ReportDefinition.Sections["ReportFooter"];
MySection.SectionFormat.EnableKeepTogether = true;
MySection.SectionFormat.EnableSuppress = false;
Listing 15-8. Modify the border of the report object State using the ReportObject data type.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.ReportObject MyObject;
MyObject = MyReport.ReportDefinition.ReportObjects["State"];
MyObject.Border.TopLineStyle = CrystalDecisions.Shared.LineStyle.SingleLine;
MyObject.Border.BottomLineStyle = CrystalDecisions.Shared.LineStyle.DoubleLine;
crystalReportViewer1.ReportSource = MyReport;
Listing 15-9. Modify the line properties of the object Line using the object’s specific data type.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.LineObject MyLine;
MyLine = (CrystalDecisions.CrystalReports.Engine.LineObject)
MyReport.ReportDefinition.ReportObjects["Line1"];
MyLine.LineStyle = CrystalDecisions.Shared.LineStyle.DashLine;
crystalReportViewer1.ReportSource = MyReport;
Listing 15-10. Modify the Text property of a TextObject located in ReportHeader.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.TextObject MyText;
MyText = (CrystalDecisions.CrystalReports.Engine.TextObject)
MyReport.ReportDefinition.ReportObjects["HeaderText"];
MyText.Text = "New Report Header";
crystalReportViewer1.ReportSource = MyReport;
Listing 15-11. Mapping the sorting and grouping objects.
private void Form1_Load(object sender, System.EventArgs e)
{
CrystalReport1 MyReport = new CrystalReport1();
MapSortFields(MyReport.DataDefinition.SortFields, 0);
Output("---",0);
MapGroups(MyReport.DataDefinition.Groups, 0);
}
public void MapSortFields(CrystalDecisions.CrystalReports.Engine.SortFields SortFields, int Indent)
{
Output("Total SortFields: " + SortFields.Count, Indent);
foreach(CrystalDecisions.CrystalReports.Engine.SortField SortField in SortFields)
{
MapSortField(SortField, Indent);
}
}
public void MapSortField(CrystalDecisions.CrystalReports.Engine.SortField SortField, int Indent)
{
MapFieldDefinition(SortField.Field, Indent);
Output("Direction: " + SortField.SortDirection.ToString(), Indent + 2);
Output("Type: " + SortField.SortType.ToString(), Indent + 2);
}
public void MapGroups(CrystalDecisions.CrystalReports.Engine.Groups Groups, int Indent)
{
Output("Total Groups: " + Groups.Count, Indent);
foreach(CrystalDecisions.CrystalReports.Engine.Group Group in Groups)
{
MapGroup(Group, Indent + 2);
}
}
public void MapGroup(CrystalDecisions.CrystalReports.Engine.Group Group, int Indent)
{
MapFieldDefinition(Group.ConditionField, Indent);
}
public void MapFieldDefinition(CrystalDecisions.CrystalReports.Engine.FieldDefinition FieldDefinition, int Indent)
{
Output("Field Name: " + FieldDefinition.Name, Indent);
Output("Formula: " + FieldDefinition.FormulaName, Indent + 2);
Output("Kind: " + FieldDefinition.Kind.ToString(), Indent + 2);
Output("Value Type: " + FieldDefinition.ValueType.ToString(), Indent + 2);
}
public void Output(string Line, int Indent)
{
lstMapping.Items.Add(new string(' ',Indent) + Line);
}
Listing 15-12. Changing the sort direction of a field
private void btnModifySorting_Click(object sender, System.EventArgs e)
{
CrystalReport1 MyReport = new CrystalReport1();
//Modify the order of the OrderDate field to Descending
//Reference the fields by traversing the sort fields collection
foreach(CrystalDecisions.CrystalReports.Engine.SortField MySortField in
MyReport.DataDefinition.SortFields)
{
if (MySortField.Field.Name == "Last Name")
{
//There are two different sort objects with this name
//Make sure its a record sort.
if (MySortField.SortType ==
CrystalDecisions.Shared.SortFieldType.RecordSortField)
{
MySortField.SortDirection =
CrystalDecisions.Shared.SortDirection.DescendingOrder;
}
}
crystalReportViewer1.ReportSource = MyReport;
}
}
Listing 15-13. Modifying a group’s field
private void btnModifyGrouping_Click(object sender, System.EventArgs e)
{
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.DatabaseFieldDefinition MyField;
//Modify the group to use CustomerName rather than Orderdate
//Get the field to base the group on
MyField = MyReport.Database.Tables["Employee"].Fields["First Name"];
//Reference the group by traversing the Groups collection
foreach(CrystalDecisions.CrystalReports.Engine.Group MyGroup in MyReport.DataDefinition.Groups)
{
if (MyGroup.ConditionField.Name == "Last Name")
{
MyGroup.ConditionField = MyField;
}
}
crystalReportViewer1.ReportSource = MyReport;
}
Listing 15-14. Open a subreport using the subreport name.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.ReportDocument MySubReport;
CrystalDecisions.CrystalReports.Engine.ReportObject MyReportObject;
CrystalDecisions.CrystalReports.Engine.TextObject MyText;
MySubReport = MyReport.OpenSubreport("Sales Subreport");
MyText = (CrystalDecisions.CrystalReports.Engine.TextObject)
MyReport.ReportDefinition.ReportObjects["Text3"];
MyText.ObjectFormat.EnableSuppress = true;
crystalReportViewer1.ReportSource = MyReport;
Listing 15-15. Find subreports using the ReportObjects collection.
CrystalReport1 MyReport = new CrystalReport1();
CrystalDecisions.CrystalReports.Engine.SubreportObject MySubreportObject;
CrystalDecisions.CrystalReports.Engine.ReportDocument MySubreport; foreach(CrystalDecisions.CrystalReports.Engine.ReportObject MyReportObject in MyReport.ReportDefinition.ReportObjects)
{
if (MyReportObject.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject){
MySubreportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)MyReportObject;
MySubreport = MySubreportObject.OpenSubreport(MySubreportObject.SubreportName);
MySubreport.RecordSelectionFormula = "";
}
}
crystalReportViewer1.ReportSource = MyReport;