ActiveReports 9 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Page Report/RDL Report Walkthroughs > Advanced > Reports with Custom Code |
This walkthrough illustrates how to create a simple report with custom code.
The walkthrough is split up into the following activities:
Note:
|
When you complete this walkthrough you get a layout that looks similar to the following at design time and at runtime.
To add an ActiveReport to the Visual Studio project
See Adding an ActiveReport to a Project for information on adding different report layouts.
To connect the report to a data source
To add a dataset
SQL Query |
Copy Code
|
---|---|
SELECT Store.StoreName, Address.City, Address.Region AS StateProvince, Address.Country, Districts.District FROM Address INNER JOIN (Districts INNER JOIN Store ON Districts.DistrictID = Store.DistrictID) ON Address.AddressID = Store.Address WHERE NOT Districts.DistrictID = 0 ORDER BY Districts.District |
To create a layout for the report
Property Name | Property Value |
---|---|
Location | 0in, 0in |
Size | 6.5in, 0.5in |
TextAlign | Center |
FontSize | 14pt |
Value | District Locations |
Property Name | Property Value | ||
---|---|---|---|
Location | 0in, 0.5in | ||
FixedSize |
6in, 7in
|
Column | Width |
---|---|
TableColumn1 | 3in |
TableColumn2 | 1.5in |
TableColumn3 | 1.5in |
=Fields!District.Value
Property Name | Property Value |
---|---|
FontSize | 12pt |
FontWeight | Bold |
BackgroundColor | MediumPurple |
Color | White |
Field | Column |
---|---|
StoreName | TableColumn1 |
City | TableColumn2 |
StateProvince | TableColumn3 |
TableColumn1
Property Name | Property Value |
---|---|
Value | Store Name |
FontWeight | Bold |
TableColumn2
Property Name | Property Value |
---|---|
Value | City |
FontWeight | Bold |
TableColumn3
Property Name | Property Value |
---|---|
Value | =iif(Fields!Country.Value="USA", "State", "Province") |
FontWeight | Bold |
Note: The expression in the third column displays the label "State" when the country is USA, and displays "Province" when it is not. |
To embed code in a report and reference it in a field expression
This custom code creates a URL to Yahoo!® Maps for each city in the report.
Visual Basic.NET code. Add to the Script tab. |
Copy Code
|
---|---|
Public Function MapLink(ByVal Country, ByVal City, ByVal StateProvince) As String Dim Link As String Dim _Country As String = Country.ToString() Dim _City As String = City.ToString() Dim _StateProvince As String = StateProvince.ToString() Select Case _Country Case "USA" Link = "http://maps.yahoo.com/maps_result?addr=&csz=" & _City & "%2C+" & _StateProvince & "&country=us&new=1&name=&qty=" Case "Canada" Link = "http:file://ca.maps.yahoo.com/maps_result?csz=%2C+" & _StateProvince & "&country=ca" Case Else Link = "" End Select Return Link End Function |
Note: Custom code is helpful if you intend to reuse code throughout the report or if code is too complex to use in an expression. Code must be instance based and written in Visual Basic.NET. You can include multiple methods, but if you want to use classes or other .NET languages, create a custom assembly. See Using Script in a Page Report for further details. |
To reference embedded code in a field expression
=Fields!City.Value
) to select it and under the Properties window, click the Property dialog link. This is a command to open the respective control's dialog. See Properties Window for more on how to access commands. .= Code.MapLink(Fields!Country.Value, Fields!City.Value, Fields!StateProvince.Value)
To view the report
OR