Info

YOrN

Boolean value to be displayed as a Y or N (Y = True, N

= False)

So, to change a Boolean field that appears on your report from displaying a binary representation to the text True/False, we could set this property to:

fieldObject.FieldFormat.BooleanFormat.OutputType = BooleanFormat.OutputType.TrueOrFalse

If you do need to see other values (such as On or Off, or Active or Inactive), you will probably want to create a formula in your report that translates these values for you:

If {Customer.Active} = True then "Active" else "Inactive"

Formatting Date Fields

Date fields within Crystal Reports have their own unique set of formatting properties that can be viewed in the Report Designer by right-clicking a date field and selecting Format. You can format date fields by example (picking a date format that looks similar to what you want), or you can customize most aspects of the date field using the dialog shown in Figure 9-10:

To bring this some of the same functionality at run time, the formatting of a date field has been broken up into multiple classes, which are all members of the DateFieldFormat class:

Property

Description

DayFormat

Returns or sets the day format

MonthFormat

Returns or sets the month format

YearFormat

Returns or sets the year format

All of the properties in these classes can be set separately so you don't need to set the month format, for example, if you only want to change how the years are displayed. To start building up a format for a date field within our report, we are going look at the day format first. For the DayFormat property, we have three options:

□ LeadingZeroNumericDay — A single digit day will be printed with a leading zero (for example, 07).

□ NumericDay—A day is printed in numeric format with no leading zero. For the MonthFormat, we have five options:

□ LeadingZeroNumericMonth — The month is printed as a number with a leading 0 for single digit months.

□ NumericMonth — The month is printed as a number with no leading 0.

□ ShortMonth — The month is printed as text in abbreviated format.

There are also three options for the YearFormat:

□ LongYear—The year is printed in long format with four digits.

□ ShortYear — The year is printed in short format with two digits.

So, to put it all together, here are some examples of how these can produce some commonly requested date formats. To display the date with the days and months with a leading zero and the full four-digit year, the code would look like this. Again, the next two code snippets are for example only and not included in a sample application of their own, but it is recommended that you experiment with these properties:

With fieldObject.FieldFormat.DateFormat

.DayFormat = DayFormat.LeadingZeroNumericDay

.MonthFormat = MonthFormat.LeadingZeroNumericMonth .YearFormat = YearFormat.LongYear

End With

The resulting date field would be displayed as "01/01/2003." For displaying only the month and year, we could change the code to read:

With fieldObject.FieldFormat.DateFormat

.DayFormat = DayFormat.NoDay

.MonthFormat = MonthFormat.LeadingZeroNumericMonth .YearFormat = YearFormat.LongYear

End With

This in turn would display the date as "01/2003." At this point, you have got to be asking yourself— how do I change the separator character? Or, if you have looked at the formatting properties associated with the field in the Report Designer, you might be wondering how you would set some of the other formatting features.

Unfortunately, the object model does not extend to cover all of the formatting features available for every type of field. If you want to change the format of a particular field and don't see the property listed, you can always create a formula based on the formula field to do the formatting work for you instead.

Formatting Time Fields

For formatting the time fields, the same concept applies, except there is only one class, TimeFieldFormat, which has the following properties:

Property

Description

AMPMFormat

Returns or sets the AM/PM type (either AMPMAfter or AMPMBefore) for 12:00am or am12:00

AMString

Returns or sets the AM string

HourFormat

Returns or sets the hour type (NoHour, NumericHour, NumericHourNoLeadingZero)

HourMinuteSeparator

Returns or sets the hour-minute separator

MinuteFormat

Returns or sets the minute type (NoMinute, NumericMinute, NumericMinuteNoLeadingZero)

MinuteSecondSeparator

Returns or sets minute-second separator

PMString

Returns or sets the PM string

SecondFormat

Returns or sets the seconds type (NumericNoSecond, NumericSecond, NumericSecondNoLeadingZero)

TimeBase

Returns or sets the time base (On12Hour, On2 4Hour)

So again, by combining all of these formatting properties, you can set the appearance for any time fields that appear in your report. For example, if you wanted to display the time in 24-hour notation, you could simply set the TimeBase property, as shown here:

fieldObject.FieldFormat.TimeFormat.TimeBase = TimeBase.On24Hour

Or, to display any times that would normally be shown as "PM" as "-Evening," you could set the PMstring as shown:

fieldObject.FieldFormat.TimeFormat.PMString = "-Evening" Which would cause the time field to read "07:13:42-Evening."

Formatting Date-Time Fields

And finally, for date-time fields, all of the classes available for both date and time fields are consolidated under the DateTimeFieldFormat class. The only addition to this class that we haven't looked at yet is the separator character that will appear between the date and time, which can be set using the DateTimeSeparator property, as shown here:

fieldObject.FieldFormat.DateTimeFormat.DateTimeSeparator = "="

If your report uses date-time fields and you would prefer not to see the date or time component, there is a setting available within the report designer to handle the way date-time fields are processed. Within the Report Designer, right-click the report and select Report ^ Report Options to open the dialog shown in Figure 9-11:

H_Report Options _©

Convert D ate-T inne Field Preview Pages Start With : pjj ^¡drf-,

G eneral S ettings-

r Convert N U LL Field Value to D erault r Show All Headers On Drill Down I Always S ort Locally v* Database Server is Case-Insensitive

Perform Grouping On Servei is? Use Indexes Or Seiver For Speed

Select Distinct Records. V Select Distinct Data for Browsing

Figure 9-11

Use the first drop-down list to select how date-time fields should be interpreted in your report. You can also set this option globally by right-clicking the report in the Report Designer and selecting Designer ^ Default Settings. Within the Reporting tab, there is an option for converting date-time fields.

"3

Formatting Currency Fields

Currency fields within Crystal Reports have a number of formatting properties that can be set to create financial reports, statements, and other fiscal information and display the data in the correct format for the type of report that is being created.

You can format a number or currency field in your report by right-clicking the field in the Report Designer and selecting Format, which will open a dialog that will allow you to format the field, and show a sample of the field in whichever format is chosen from the list. Or, you can click the Customize button to control the granular properties associated with formatting, as shown in Figure 9-12.

Figure 9-12

Unfortunately, not all of these properties can be changed programmatically, but the following properties of the NumericFieldFormat class are supported:

Property

Description

CurrencySymbolFormat

Returns or sets the currency symbol type (FixedSymbol, FloatingSymbol, or NoSymbol).

DecimalPlaces

Returns or sets the number of decimal places

EnableUseLeadingZero

Returns or sets the option to use a leading zero for decimal values

NegativeFormat

Returns or sets the negative format type (Bracketed, LeadingMinus, NotNegative, TrailingMinus).

RoundingFormat

Returns or sets the rounding format type (To see all of the different types available, search the Visual Studio Combined Help for "RoundingFormat Enumeration")

So, to change the format of a numeric field, showing a fixed currency symbol with two decimal places and rounding to the second decimal place (tenth), the code would look like this (again, for illustration only and not included in the sample application):

With fieldObject.FieldFormat.NumericFormat

.CurrencySymbolFormat = CurrencySymbolFormat.FixedSymbol .DecimalPlaces = 2

.RoundingFormat = RoundingFormat.RoundToTenth End With

Keep in mind that we don't have the ability to actually change the currency symbol itself through the object model. You will need to set this in the report design itself or use a formula (for instance, If {Customer.Country} = "USA" then "US$" else "UKP") and position the formula immediately before the numeric field (or use yet another formula to concatenate the currency symbol formula and the field itself together).

Was this article helpful?

0 0

Post a comment