{"id":646,"date":"2008-01-04T19:48:09","date_gmt":"2008-01-05T02:48:09","guid":{"rendered":"http:\/\/members.crystalreportsbook.com\/crystal-reports-xi\/1511-c-code-listings\/"},"modified":"2008-01-04T19:48:09","modified_gmt":"2008-01-05T02:48:09","slug":"1511-c-code-listings","status":"publish","type":"post","link":"http:\/\/www.crystalreportsonlinetraining.com\/training\/1511-c-code-listings\/","title":{"rendered":"15.11 C# Code Listings"},"content":{"rendered":"<h1>C# Code Listings<\/h1>\n<p>The C# code listings are equivalent to the VB.NET code listings.<\/p>\n<p>         <code_Caption>Listing 15-1. Changing the selection formula with the ReportDocument object.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>MyReport.DataDefinition.RecordSelectionFormula =<\/code><br \/>\n         <code>\"{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)\";<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><br \/>\n         <code_Caption>Listing 15-2. Changing the selection formula with the viewer control.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><br \/>\n         <code>crystalReportViewer1.SelectionFormula =<\/code><br \/>\n         <code>\"{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 02, 20)<\/code><br \/>\n         <code_Caption>Listing 15-3. Mapping the ReportDefinition object model.<\/code_Caption><br \/>\n         <code>private void Form1_Load(object sender, System.EventArgs e)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>MapAreas(MyReport.ReportDefinition.Areas, 0);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapAreas(CrystalDecisions.CrystalReports.Engine.Areas Areas, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.Area Area in Areas)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapArea(Area, Indent);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapSections(CrystalDecisions.CrystalReports.Engine.Sections Sections, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.Section Section in Sections)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapSection(Section, Indent);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapReportObjects(CrystalDecisions.CrystalReports.Engine.ReportObjects ReportObjects, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.ReportObject ReportObject in ReportObjects)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapReportObject(ReportObject, Indent + 2);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapArea(CrystalDecisions.CrystalReports.Engine.Area Area, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>Output(\"Area: \" + Area.Name, Indent);<\/code><br \/>\n         <code>Output(\"Kind: \" + Area.Kind.ToString(), Indent+2);<\/code><br \/>\n         <code>Output(\"Suppress: \" + Area.AreaFormat.EnableSuppress.ToString(), Indent + 2);<\/code><br \/>\n         <code>Output(\"Keep Together: \" + Area.AreaFormat.EnableKeepTogether.ToString(), Indent + 2);<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.Section Section in Area.Sections)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapSection(Section, Indent + 2);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapSection(CrystalDecisions.CrystalReports.Engine.Section Section, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>\/\/Print the Section properties<\/code><br \/>\n         <code>Output(\"Section: \" + Section.Name, Indent);<\/code><br \/>\n         <code>Output(\"Suppress: \" + Section.SectionFormat.EnableSuppress.ToString(), Indent);<\/code><br \/>\n         <code>Output(\"Keep Together: \" + Section.SectionFormat.EnableKeepTogether.ToString(), Indent);<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.ReportObject ReportObject in Section.ReportObjects)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapReportObject(ReportObject, Indent + 2);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapReportObject(CrystalDecisions.CrystalReports.Engine.ReportObject ReportObject, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>switch(ReportObject.Kind)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>case CrystalDecisions.Shared.ReportObjectKind.TextObject:<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapTextObject(((CrystalDecisions.CrystalReports.Engine.TextObject)ReportObject), Indent);<\/code><br \/>\n         <code>break;<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>case CrystalDecisions.Shared.ReportObjectKind.FieldObject:<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapFieldObject(((CrystalDecisions.CrystalReports.Engine.FieldObject)ReportObject), Indent);<\/code><br \/>\n         <code>break;<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapTextObject(CrystalDecisions.CrystalReports.Engine.TextObject TextObject, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>Output(\"Name: \" + TextObject.Name, Indent);<\/code><br \/>\n         <code>Output(\"Kind: \" + TextObject.Kind.ToString(), Indent + 2);<\/code><br \/>\n         <code>Output(\"Text: \" + TextObject.Text, Indent + 2);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapFieldObject(CrystalDecisions.CrystalReports.Engine.FieldObject FieldObject, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>Output(\"Name: \" + FieldObject.Name, Indent);<\/code><br \/>\n         <code>Output(\"Kind: \" + FieldObject.Kind.ToString(), Indent + 2);<\/code><br \/>\n         <code>Output(\"DataSource.Name: \" + FieldObject.DataSource.Name, Indent + 2);<\/code><br \/>\n         <code>Output(\"DataSource.Formula: \" + FieldObject.DataSource.FormulaName, Indent + 2);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void Output(string Line, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>lstMapping.Items.Add(new string(' ',Indent) + Line);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code_Caption>Listing 15-4. Get a reference to the ReportFooter section.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.Section MySection;<\/code><br \/>\n         <code>MySection = MyReport.ReportDefinition.Sections[\"ReportFooter\"];<\/code><br \/>\n         <code_Caption>Listing 15-5. Get a reference to the report object State using the ReportObject data type.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.ReportObject MyObject;<\/code><br \/>\n         <code>MyObject = MyReport.ReportDefinition.ReportObjects[\"State\"];<\/code><br \/>\n         <code_Caption>Listing 15-6. Get a reference to the State object using the object&#8217;s specific data type.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.FieldObject MyField;<\/code><br \/>\n         <code>MyField = (CrystalDecisions.CrystalReports.Engine.FieldObject)<\/code><br \/>\n         <code>MyReport.ReportDefinition.ReportObjects[\"State\"];<\/code><br \/>\n         <code_Caption>Listing 15-7. Modify properties of the ReportFooter section.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.Section MySection;<\/code><br \/>\n         <code>MySection = MyReport.ReportDefinition.Sections[\"ReportFooter\"];<\/code><br \/>\n         <code>MySection.SectionFormat.EnableKeepTogether = true;<\/code><br \/>\n         <code>MySection.SectionFormat.EnableSuppress = false;<\/code><br \/>\n         <code_Caption>Listing 15-8. Modify the border of the report object State using the ReportObject data type.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.ReportObject MyObject;<\/code><br \/>\n         <code>MyObject = MyReport.ReportDefinition.ReportObjects[\"State\"];<\/code><br \/>\n         <code>MyObject.Border.TopLineStyle = CrystalDecisions.Shared.LineStyle.SingleLine;<\/code><br \/>\n         <code>MyObject.Border.BottomLineStyle = CrystalDecisions.Shared.LineStyle.DoubleLine;<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><br \/>\n         <code_Caption>Listing 15-9. Modify the line properties of the object Line using the object&#8217;s specific data type.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.LineObject MyLine;<\/code><br \/>\n         <code>MyLine = (CrystalDecisions.CrystalReports.Engine.LineObject)<\/code><br \/>\n         <code>MyReport.ReportDefinition.ReportObjects[\"Line1\"];<\/code><br \/>\n         <code>MyLine.LineStyle = CrystalDecisions.Shared.LineStyle.DashLine;<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><br \/>\n         <code_Caption>Listing 15-10. Modify the Text property of a TextObject located in ReportHeader.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.TextObject MyText;<\/code><br \/>\n         <code>MyText = (CrystalDecisions.CrystalReports.Engine.TextObject)<\/code><br \/>\n         <code>MyReport.ReportDefinition.ReportObjects[\"HeaderText\"];<\/code><br \/>\n         <code>MyText.Text = \"New Report Header\";<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><br \/>\n         <code_Caption>Listing 15-11. Mapping the sorting and grouping objects.<\/code_Caption><br \/>\n         <code>private void Form1_Load(object sender, System.EventArgs e)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>MapSortFields(MyReport.DataDefinition.SortFields, 0);<\/code><br \/>\n         <code>Output(\"---\",0);<\/code><br \/>\n         <code>MapGroups(MyReport.DataDefinition.Groups, 0);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapSortFields(CrystalDecisions.CrystalReports.Engine.SortFields SortFields, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>Output(\"Total SortFields: \" + SortFields.Count, Indent);<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.SortField SortField in SortFields)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapSortField(SortField, Indent);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapSortField(CrystalDecisions.CrystalReports.Engine.SortField SortField, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapFieldDefinition(SortField.Field, Indent);<\/code><br \/>\n         <code>Output(\"Direction: \" + SortField.SortDirection.ToString(), Indent + 2);<\/code><br \/>\n         <code>Output(\"Type: \" + SortField.SortType.ToString(), Indent + 2);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapGroups(CrystalDecisions.CrystalReports.Engine.Groups Groups, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>Output(\"Total Groups: \" + Groups.Count, Indent);<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.Group Group in Groups)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapGroup(Group, Indent + 2);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapGroup(CrystalDecisions.CrystalReports.Engine.Group Group, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MapFieldDefinition(Group.ConditionField, Indent);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void MapFieldDefinition(CrystalDecisions.CrystalReports.Engine.FieldDefinition FieldDefinition, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>Output(\"Field Name: \" + FieldDefinition.Name, Indent);<\/code><br \/>\n         <code>Output(\"Formula: \" + FieldDefinition.FormulaName, Indent + 2);<\/code><br \/>\n         <code>Output(\"Kind: \" + FieldDefinition.Kind.ToString(), Indent + 2);<\/code><br \/>\n         <code>Output(\"Value Type: \" + FieldDefinition.ValueType.ToString(), Indent + 2);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>public void Output(string Line, int Indent)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>lstMapping.Items.Add(new string(' ',Indent) + Line);<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code_Caption>Listing 15-12. Changing the sort direction of a field<\/code_Caption><br \/>\n         <code>private void btnModifySorting_Click(object sender, System.EventArgs e)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>\/\/Modify the order of the OrderDate field to Descending<\/code><br \/>\n         <code>\/\/Reference the fields by traversing the sort fields collection<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.SortField MySortField in<\/code><br \/>\n         <code>MyReport.DataDefinition.SortFields)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>if (MySortField.Field.Name == \"Last Name\")<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>\/\/There are two different sort objects with this name<\/code><br \/>\n         <code>\/\/Make sure its a record sort.<\/code><br \/>\n         <code>if (MySortField.SortType ==<\/code><br \/>\n         <code>CrystalDecisions.Shared.SortFieldType.RecordSortField)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MySortField.SortDirection =<\/code><br \/>\n         <code>CrystalDecisions.Shared.SortDirection.DescendingOrder;<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code_Caption>Listing 15-13. Modifying a group&#8217;s field<\/code_Caption><br \/>\n         <code>private void btnModifyGrouping_Click(object sender, System.EventArgs e)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.DatabaseFieldDefinition MyField;<\/code><br \/>\n         <code>\/\/Modify the group to use CustomerName rather than Orderdate<\/code><br \/>\n         <code>\/\/Get the field to base the group on<\/code><br \/>\n         <code>MyField = MyReport.Database.Tables[\"Employee\"].Fields[\"First Name\"];<\/code><br \/>\n         <code>\/\/Reference the group by traversing the Groups collection<\/code><br \/>\n         <code>foreach(CrystalDecisions.CrystalReports.Engine.Group MyGroup in MyReport.DataDefinition.Groups)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>if (MyGroup.ConditionField.Name == \"Last Name\")<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>MyGroup.ConditionField = MyField;<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code_Caption>Listing 15-14. Open a subreport using the subreport name.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.ReportDocument MySubReport;<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.ReportObject MyReportObject;<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.TextObject MyText;<\/code><br \/>\n         <code>MySubReport = MyReport.OpenSubreport(\"Sales Subreport\");<\/code><br \/>\n         <code>MyText = (CrystalDecisions.CrystalReports.Engine.TextObject)<\/code><br \/>\n         <code>MyReport.ReportDefinition.ReportObjects[\"Text3\"];<\/code><br \/>\n         <code>MyText.ObjectFormat.EnableSuppress = true;<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><br \/>\n         <code_Caption>Listing 15-15. Find subreports using the ReportObjects collection.<\/code_Caption><br \/>\n         <code>CrystalReport1 MyReport = new CrystalReport1();<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.SubreportObject MySubreportObject;<\/code><br \/>\n         <code>CrystalDecisions.CrystalReports.Engine.ReportDocument MySubreport;               foreach(CrystalDecisions.CrystalReports.Engine.ReportObject MyReportObject in MyReport.ReportDefinition.ReportObjects)<\/code><br \/>\n         <code>{<\/code><br \/>\n         <code>if (MyReportObject.Kind == CrystalDecisions.Shared.ReportObjectKind.SubreportObject){<\/code><br \/>\n         <code>MySubreportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)MyReportObject;<\/code><br \/>\n         <code>MySubreport = MySubreportObject.OpenSubreport(MySubreportObject.SubreportName);<\/code><br \/>\n         <code>MySubreport.RecordSelectionFormula = \"\";<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>}<\/code><br \/>\n         <code>crystalReportViewer1.ReportSource = MyReport;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 = &#8220;{Orders.Order Date} in Date (1996, 02, 19) to Date (1996, 03, 28)&#8221;; crystalReportViewer1.ReportSource = MyReport; Listing 15-2. Changing the selection formula with the viewer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,2],"tags":[],"class_list":["post-646","post","type-post","status-publish","format-standard","hentry","category-chapter-15-runtime-customization","category-crystal-reportsnet-2003","entry"],"_links":{"self":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/646","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/comments?post=646"}],"version-history":[{"count":0,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/646\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/media?parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/categories?post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/tags?post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}