Bind the TextBox Format Property to a String Report Parameter
Environment
| Version | Product | Author |
|---|
| 20.2.26.812 | Telerik Reporting |Desislava Yordanova|
Description
You can bind the Format property of a TextBox to a string report parameter. This allows the format to be changed without modifying the report item. The report parameter value must be a .NET composite format string, such as {0:#,##0.00}.
The Format property does not accept a report expression directly. Use a binding with the property path Format and the expression =Parameters.NumberFormat.Value instead.
Solution
Configure the report in the Report Designer
-
Add a report parameter named
NumberFormat. -
Set the parameter Type to
String. -
Set the parameter Value to
{0:#,##0.00}. -
Select the
TextBoxthat displays the value and set its Value to a field expression, such as=Fields.Amount. -
Open the
TextBoxBindings property and add a binding with the following settings:Binding setting Value Property path FormatExpression =Parameters.NumberFormat.Value
When the report renders, a value such as 1234567.89 is formatted with two decimal places and a grouping separator according to the report culture.
Configure the report programmatically
The following example creates a string report parameter and binds the TextBox.Format property to that parameter:
var report = new Telerik.Reporting.Report();
report.ReportParameters.Add(
"NumberFormat",
Telerik.Reporting.ReportParameterType.String,
"{0:#,##0.00}");
var textBox = new Telerik.Reporting.TextBox
{
Value = "=Fields.Amount"
};
textBox.Bindings.Add(new Telerik.Reporting.Binding
{
Path = "Format",
Expression = "=Parameters.NumberFormat.Value"
});
var detailSection = new Telerik.Reporting.DetailSection();
detailSection.Items.Add(textBox);
report.Items.Add(detailSection);
The binding expression returns the value of the NumberFormat report parameter. The TextBox.Value expression supplies the value that the composite format string formats.
Apply the format to multiple report items
You can use the same string report parameter to control the format of multiple text items. Add the following binding to every TextBox that should use the shared format:
| Binding setting | Value |
|---|---|
| Property path | Format |
| Expression | =Parameters.NumberFormat.Value |
For example, add this binding to both an amount text box and a total text box. When you change the NumberFormat parameter from {0:#,##0.00} to {0:C2}, both text boxes use the new format the next time the report is processed. You only need to change the parameter value; you do not need to edit each bound text item.
The shared format applies only to text items that use this binding. Items without the binding retain their own format.
Format string examples
| Parameter value | Result for 1234567.89 |
|---|---|
{0:0.00} | 1234567.89 |
{0:#,##0.00} | 1,234,567.89 |
{0:C2} | Currency output according to the report culture |
The grouping separator and decimal symbol depend on the culture used to render the report.
Troubleshooting
- Set the binding Property path to
Format, notPropertyPath. - Set the binding Expression to
=Parameters.NumberFormat.Value. - Ensure that the report parameter name matches the name used in the expression.
- Ensure that the report parameter is a
Stringand contains a valid composite format string. - Keep the numeric value in
TextBox.Value; theFormatbinding controls how that value is displayed.
When inspecting bindings in code, use the Binding.Path property to read the target path. The Binding class does not expose a PropertyPath property.