Progress® Telerik® Reporting R2 2017

Accessing Report Items Programmatically

In the examples below we show how to access a report item from within the report and from the invoking application. In the first example let's access a TextBox item from the detail section ItemDataBinding eventhandler.

Access Report Items from within events

  1. Select the detail section in the designer, and in the Properties Window, Events tab, find the ItemDataBinding entry and double-click.
  2. The event handler passes in "sender" which is a Telerik.Reporting.Processing.DetailSection that represents the section during processing. Thus you can derive:

    • A DataObject that represents the underlying data for a record being processed.
    • All processing report items.
  3. Use the ElementTreeHelper.GetChildByName method to locate an item on the report. In the code example below GetChildByName is used to retrieve "textBox1" TextBox.
Note Note

In the code example below we are referring to a Processing.TextBox, not Report1.TextBox.  The Processing.TextBox represents the TextBox during processing and has a different set of properties than the Report1.TextBox used when defining the report.

Example

private void detail_ItemDataBinding(object sender, EventArgs e)
{
    Telerik.Reporting.Processing.DetailSection section = (sender as Telerik.Reporting.Processing.DetailSection);
    Telerik.Reporting.Processing.TextBox txt = (Telerik.Reporting.Processing.TextBox)Telerik.Reporting.Processing.ElementTreeHelper.GetChildByName(section, "textBox1");
    object title = section.DataObject["Title"];
    if ((string)title == "Developer")
    {
        txt.Style.BackgroundColor = System.Drawing.Color.Blue;
    }
}

 Access items from calling application

If we are in the context of a web/win form and we need to access an item from the Report that is shown in a ReportViewer control, we can proceed directly following the report hierarchy. We use a report source object of the same type as the report source assigned to the ReportViewer control. Consider the following code:

protected void Button1_Click(object sender, EventArgs e)
{
    Telerik.Reporting.InstanceReportSource instanceReportSource = (Telerik.Reporting.InstanceReportSource)this.reportViewer1.ReportSource;
    Telerik.Reporting.Report report = (Telerik.Reporting.Report)instanceReportSource.ReportDocument;
    Telerik.Reporting.TextBox txt = report.Items.Find("productNameDataTextBox", true)[0] as Telerik.Reporting.TextBox;
}

 Access report fields from a Table item

You can reference the report fields from a table item easily using the Report API hierarchy. Consider the following code:

private void tableTextBox_ItemDataBinding(object sender, EventArgs eventArgs)
{
    //get the textbox from the sender object            
    Telerik.Reporting.Processing.TextBox textBox = (Telerik.Reporting.Processing.TextBox)sender;
    //get the table object
    Telerik.Reporting.Processing.Table table = (Telerik.Reporting.Processing.Table)textBox.Parent;
    //get the detail section
    Telerik.Reporting.Processing.DetailSection detail = (Telerik.Reporting.Processing.DetailSection)table.Parent;
    //get the raw value from the Report datasource directly
    textBox.Value = detail.DataObject["Data"];
}