Using the Events of Report Items
Each report item on a report exposes these events:
Event | Description |
---|---|
ItemDataBinding | Fires just before the item is bound to data. |
ItemDataBound | Fires just after the item is bound to data. |
The Data Items expose one additional event: NeedDataSource. This event fires when the item does not have data source bound to it. For example, the SubReport NeedDataSource event will fire when SubReport.ReportSource does not have data source.
Let us demonstrate this with a simple example. Suppose that we have a TextBox containing the job position of an employee. Assume that the TextBox's Value property equals "=Fields.Title", where Title is a column from the data source containing values like "Team Leader", "Senior Developer", "Junior Developer", "Quality Assurance", etc. For the sake of our example, suppose that we would like to color all developers in Blue.
First we need to attach to the TextBox's ItemDataBinding event. Double-click the event in the property grid and an event handler method will be automatically generated for you.
As explained in Understanding Events topic, the sender argument is in fact the processing TextBox that has already been data bound. The method will be called for each data row, i.e. for each employee we have in the data source. So the only thing we have left to do is cast the sender, check the position, and do the coloring if needed:
private void textBox1_ItemDataBinding(object sender, EventArgs e)
{
Telerik.Reporting.Processing.TextBox txt = (Telerik.Reporting.Processing.TextBox)sender;
Telerik.Reporting.Processing.IDataObject dataObject = (Telerik.Reporting.Processing.IDataObject)txt.DataObject;
if ((string)dataObject["Title"] == "Developer")
{
txt.Style.BackgroundColor = System.Drawing.Color.Blue;
}
}
Private Sub textBox1_ItemDataBinding(sender As Object, e As EventArgs) Handles textBox1.ItemDataBinding
Dim txt As Telerik.Reporting.Processing.TextBox = DirectCast(sender, Telerik.Reporting.Processing.TextBox)
Dim dataObject As Telerik.Reporting.Processing.IDataObject = DirectCast(txt.DataObject, Telerik.Reporting.Processing.IDataObject)
If DirectCast(dataObject("Title"), String) = "Developer" Then
txt.Style.BackgroundColor = System.Drawing.Color.Blue
End If
End Sub
The provided example can be easily implemented using Conditional Formatting, see Change row color based on a value in the row or Bindings.