Formatting Values with C# in a DevExpress Report Printable for Creatio (formerly bpm’online)

One of my favorite things you can do in a DevExpress report for bpm’online is using C# to manipulate your data and format it, combine values, etc before the report is rendered.

When a DevExpress report is run, every object you’ve placed on the report has an OnBeforePrint event. This event fires after the object has been bound to the value, but before the report is rendered. You can use this event to change the data however you’d like. For example, you have a DateTime value, but you only want to display the date. To do this, select the object you added to the report for the DateTime value. If you added this by simply dragging it from the Data pane (on the right side with the properties) onto the report, you actually have a label, bound to the DateTime property in the object. Select it, then expand the Scripts property in the Behaviors section, then use the dropdown for Before Print, and click New.

In the method that is created in the code editor, add the following:

// first get the label
var label = (XRLabel)sender;

// now get the value from the label and change it
DateTime date;
if (DateTime.TryParse(label.Text, out date)) 
    // now set back as the Text for the label
    label.Text = date.ToShortDateString();

Pretty simple. You can see we’re just getting the label itself, then getting it’s value and setting it again. How do we know the label’s type is “XRLabel”? Well, it just is, but there’s other types of elements you can add to a DevExpress report. You can see the complete list of components available, as well as their properties/methods, in the DevExpress docs. Note that not all of those are implemented in the bpm’online implementation of DevExpress reports. If you’re not sure what the control type is, simply wire up the Before Print for the control and just throw the control as an Exception or try to cast it as an XRLabel. Then when you run the report, of course it will error, but you’ll be able to see the error details in the browser’s network tab in Dev Tools so see the error details when you run the report. The Exception details will show you the Exception you threw, and it will give you the control type. Hacky, I know, but it’s a way to get what you need.

You can do more than the above in these events too. Often, when I need to display an address, I will combine all the address fields into a single string, then bind the string to a label. This way, you don’t have empty spaces for missing address values. To do this, we’ll drag one of the address fields onto the report, then wire up it’s Before Print event. Now, in the Before Print, we can get the label, like we did in the previous event, but also get other values in the report data using a method named “GetCurrentColumnValue”. Here’s a code sample:

var label = (XRLabel)sender;

var addr1 = GetCurrentColumnValue("Address");
var city = GetCurrentColumnValue("City.Name");
var state = GetCurrentColumnValue("Region.Name");
var country = GetCurrentColumnValue("Country.Name");
var zip = GetCurrentColumnValue("Zip");

var fullAddr = // combine into one address string with line breaks as needed
               // these are objects, so you’ll need to check if not null and then
               // use ToString()

label.Text = fullAddr;

Of course, to use GetCurrentColumnValue, the column you’re getting needs to be added to the report’s Data.

One important thing to note, GetCurrentColumnValue is only available in the main report group headers, detail, etc. If you’ve added a DetailReport (which is sort of like a sub-report), you’ll need to use GetCurrentRow to get other values instead of GetCurrentColumnValue. Using GetCurrentRow will give you the DataRow for the current row, then you can get any values needed from there. To use GetCurrentRow, you’ll reference the DetailReport you’re in (in the sample below, the DetailReport’s name is DetailReport1), like this. Also, you’ll need to cast it as a DataRowView, then get the DataRow:

var row = ((DataRowView)DetailReport1.GetCurrentRow()).Row;
var someValue = row["MyFieldName"].ToString(); // etc...

If you’re doing this in a BeforePrint event of a label or other control, you can dynamically get the DetailReport from it, like this:

var label = (XRLabel)sender;
var row = ((DataRowView)label.Report.GetCurrentRow()).Row;

This approach allows you to reuse the code for the BeforePrint of items in other DetailReports in the report as well.

Want content like this delivered to your inbox? Sign up for our newsletter!

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

Submit a Comment

Your email address will not be published. Required fields are marked *