{"id":813,"date":"2008-03-10T19:51:39","date_gmt":"2008-03-11T02:51:39","guid":{"rendered":"http:\/\/members.crystalreportsbook.com\/books\/?p=813"},"modified":"2010-11-17T19:11:39","modified_gmt":"2010-11-18T02:11:39","slug":"16-8-programming-the-crystalreportviewer-control","status":"publish","type":"post","link":"http:\/\/www.crystalreportsonlinetraining.com\/training\/16-8-programming-the-crystalreportviewer-control\/","title":{"rendered":"16.08 Programming the CrystalReportViewer Control"},"content":{"rendered":"<h2>Programming the CrystalReportViewer Control<\/h2>\n<p>As mentioned earlier in this chapter, the viewer control should only be used to modify the display properties of the viewer and shouldn&#8217;t be relied upon to make report changes. However, the viewer does have this functionality built into it, so we&#8217;ll cover it here so that you know how to use it if the need arises.<\/p>\n<p>Programming with the viewer has some similarities to programming with the ReportDocument class, but there are differences. From a high level, the viewer is similar because you have to get a reference to the parameter object and assign a value to its CurrentValues collection. From an implementation standpoint, the viewer is different because the names of the parameter classes are different.<\/p>\n<p>The viewer is also different in how you assign the current\/default value collection. It requires that you override the existing values collection with the new values collection. The ReportDocument class has you call the ApplyCurrentValue() method to do this.<\/p>\n<p>The viewer uses the ParameterFields collection to manage the properties of each parameter in a report. The ParameterFields collection manages the ParameterField object.<\/p>\n<p>Programming with the CrystalReportViewer consists of 8 steps:<\/p>\n<p>         <num_List_First>Assign the report to the ReportSource property of the viewer.<\/num_List_First><br \/>\n         <num_List>Get a reference to the ParameterField object via the ParameterFieldInfo collection.<\/num_List><br \/>\n         <num_List>Instantiate a parameter value object. This will either be the ParameterDiscreteValue class or the ParameterRangeValue class.<\/num_List><br \/>\n         <num_List>Set the properties of the new parameter value object.<\/num_List><br \/>\n         <num_List>Use the ParameterValues.Add() method to add the parameter value to collection of values.<\/num_List><br \/>\n         <num_List>Override the CurrentValues collection with the new ParameterValues collection.<\/num_List><br \/>\n         <caution>In Step 2 (getting a reference to the ParameterField object), if you are using the viewer object and programming in C#, then the parameter name is case sensitive. Passing a string to the ParameterFieldInfo() indexer that doesn&#8217;t match the parameter name casing results in the error System.ArgumentOutOfRange being raised.<\/caution><\/p>\n<p>Listing 16-8 illustrates these steps. It creates a parameter that is a range value and assigns it to the CurrentValues collection. The in-line comments explain the steps that are taking place.<\/p>\n<p>         <b>Listing 16-8. Use the viewer to modify a range parameter.<\/b><br \/>\n         <b>[VB.NET]<\/b><br \/>\n         <code>Dim myParameterValues as New CrystalDecisions.Shared.ParameterValues<\/code><br \/>\n         <code>Dim myParameterField As CrystalDecisions.Shared.ParameterField<\/code><br \/>\n         <code>Dim myParameterRangeValue As CrystalDecisions.Shared.ParameterRangeValue<\/code><br \/>\n         <code>'Step 1: Assign the report object to the viewer<\/code><br \/>\n         <code>CrystalReportViewer1.ReportSource = Server.MapPath(\"Invoice.rpt\")<\/code><br \/>\n         <code>'Step 2: Reference the ParameterField object<\/code><br \/>\n         <code>myParameterField = CrystalReportViewer1.ParameterFieldInfo(\"DateRange\")<\/code><br \/>\n         <code>'Step 3: Create a ParameterValue object.<\/code><br \/>\n         <code>myParameterRangeValue = New CrystalDecisions.Shared.ParameterRangeValue<\/code><br \/>\n         <code>'Step 4: Assign a value to the object<\/code><br \/>\n         <code>myParameterRangeValue.StartValue = DateTime.Parse(\"1\/4\/1997\")<\/code><br \/>\n         <code>myParameterRangeValue.EndValue = DateTime.Parse(\"2\/2\/1997\")<\/code><br \/>\n         <code>'Step 5: Add the ParameterValue object to the values collection<\/code><br \/>\n         <code>myParameterValues.Add(myParameterRangeValue)<\/code><br \/>\n         <code>'Step 6: Override the CurrentValues collection with the new values collection<\/code><br \/>\n         <code>myParameterField.CurrentValues = myParameterRangeValue<\/code><br \/>\n         <b>[C#]<\/b><br \/>\n         <code>CrystalDecisions.Shared.ParameterFields ParameterFields;<\/code><br \/>\n         <code>CrystalDecisions.Shared.IParameterField ParameterField;<\/code><br \/>\n         <code>CrystalDecisions.Shared.ParameterRangeValue ParameterRangeValue;<\/code><br \/>\n         <code>\/\/Step 1: Assign the report object to the viewer<\/code><br \/>\n         <code>CrystalReportViewer1.ReportSource = Server.MapPath(\"Invoice.rpt\");<\/code><br \/>\n         <code>\/\/Step 2: Reference the ParameterField object<\/code><br \/>\n         <code>ParameterFields = crystalReportViewer1.ParameterFieldInfo[\"DateRange\"];<\/code><br \/>\n         <code>\/\/Step 3: Create a ParameterValue object<\/code><br \/>\n         <code>ParameterRangeValue = new CrystalDecisions.Shared.ParameterRangeValue();<\/code><br \/>\n         <code>\/\/Step 4: Assign a value to the object<\/code><br \/>\n         <code>ParameterRangeValue.StartValue = DateTime.Parse(\"1\/4\/1997\");<\/code><br \/>\n         <code>ParameterRangeValue.EndValue = DateTime.Parse(\"1\/20\/1997\");<\/code><br \/>\n         <code>\/\/Step 6: Override the CurrentValues collection with the new values collection<\/code><br \/>\n         <code>ParameterField.CurrentValues = ParameterRangeValue;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Programming the CrystalReportViewer Control As mentioned earlier in this chapter, the viewer control should only be used to modify the display properties of the viewer and shouldn&#8217;t be relied upon to make report changes. However, the viewer does have this functionality built into it, so we&#8217;ll cover it here so that you know how to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,40],"tags":[],"class_list":["post-813","post","type-post","status-publish","format-standard","hentry","category-chapter-16-modifying-formulas-and-parameters","category-crystal-reportsnet-2008","entry"],"_links":{"self":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/813","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=813"}],"version-history":[{"count":1,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/813\/revisions"}],"predecessor-version":[{"id":1795,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/813\/revisions\/1795"}],"wp:attachment":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/media?parent=813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/categories?post=813"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/tags?post=813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}