From d0c46f89f60d6dd10d4439b3ba6fdbc388b2c3e3 Mon Sep 17 00:00:00 2001 From: KB Bot Date: Mon, 21 Jul 2025 15:15:28 +0000 Subject: [PATCH 1/2] Added new kb article accessing-values-winforms-report-viewer --- ...accessing-values-winforms-report-viewer.md | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 knowledge-base/accessing-values-winforms-report-viewer.md diff --git a/knowledge-base/accessing-values-winforms-report-viewer.md b/knowledge-base/accessing-values-winforms-report-viewer.md new file mode 100644 index 000000000..c9d100ec9 --- /dev/null +++ b/knowledge-base/accessing-values-winforms-report-viewer.md @@ -0,0 +1,100 @@ +--- +title: Accessing Data from Rendered Report in WinForms Viewer +description: Learn how to access values from a rendered TRDP or TRDX report in the WinForms Report Viewer by using report parameters or report events. +type: how-to +page_title: Extract Values from WinForms Report Viewer Processed Report +meta_title: Extract Values from WinForms Report Viewer Processed Report +slug: accessing-values-winforms-report-viewer +tags: reporting, winforms, report viewer, parameters, report items +res_type: kb +ticketid: 1693205 +--- + +## Environment + + + + + + + + + + + +
ProductReporting
Version19.1.25.521
+ +## Description + +I need to display a TRDP or TRDX report in the WinForms Report Viewer, click a button, and extract specific values (e.g., client email) from the rendered report. Directly casting a `UriReportSource` to an `InstanceReportSource` results in an invalid cast error. Additionally, I need access to the processed report or the report's data source fields. + +This knowledge base article also answers the following questions: +- How to get data from a rendered report in WinForms Report Viewer? +- How to use report parameters to retrieve values from a report? +- How to use report events to access processed report values? + +## Solution + +### Using Report Parameters + +Use report parameters to retrieve values from the report. Connect the report parameter to the desired field in the report data source. + +1. Set up a report parameter in the report designer and bind its value to the required field: + ``` + = Fields.MyField + ``` + +2. Access the report parameter value in your application code: + ```vb + Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click + Dim reportParameterValue = Me.TKReportViewer.ReportSource.Parameters("MyParameter").Value + End Sub + ``` + +This approach is simple and clean for retrieving individual values from the report. + +### Using Report Events + +For scenarios requiring multiple values from the processed report, use report events: + +1. Unpackage the TRDP file and assign custom event handlers to the report: + ```vb + Dim uri As String = "C:\Path\To\Report.trdp" + Dim reportPackager As New ReportPackager() + Dim instanceReportSource As New InstanceReportSource() + + Using sourceStream As FileStream = File.OpenRead(uri) + Dim report As Report = CType(reportPackager.UnpackageDocument(sourceStream), Report) + Dim table As Table = CType(report.Items.Find("table1", True)(0), Table) + Dim detailSection As DetailSection = CType(report.Items.Find("detailSection1", True)(0), DetailSection) + + AddHandler report.ItemDataBound, AddressOf Report_ItemDataBound + AddHandler table.ItemDataBound, AddressOf Table_ItemDataBound + + instanceReportSource.ReportDocument = report + End Using + ``` + +2. Define the event handler to process and retrieve data: + ```vb + Private Sub Table_ItemDataBound(sender As Object, e As EventArgs) + Dim processingTable As Telerik.Reporting.Processing.Table = CType(sender, Telerik.Reporting.Processing.Table) + For Each row As Telerik.Reporting.Processing.TableRow In processingTable.Rows + Dim firstCell As Telerik.Reporting.Processing.TableCell = row.GetCell(0) + Dim textBoxItem As Telerik.Reporting.Processing.TextBox = CType(firstCell.Item, Telerik.Reporting.Processing.TextBox) + Trace.WriteLine(String.Format("Row: {0}, Cell: {1}, Text: {2}", row.Index, firstCell.RowIndex, textBoxItem.Value)) + Next + End Sub + ``` + +3. Use the event handlers to extract values from the report detail section or table. + +This approach is suitable for retrieving multiple records or detailed data from the processed report. + +## See Also + +- [UriReportSource Documentation](https://docs.telerik.com/reporting/api/telerik.reporting.urireportsource) +- [InstanceReportSource Documentation](https://docs.telerik.com/reporting/api/telerik.reporting.instancereportsource) +- [Report Parameters Overview](https://docs.telerik.com/reporting/designing-reports/connecting-to-data/report-parameters/overview) +- [Unpackaging Report Definitions](https://docs.telerik.com/reporting/embedding-reports/program-the-report-definition/package-report-definition#unpackaging) +- [Report Events Overview](https://docs.telerik.com/reporting/embedding-reports/program-the-report-definition/report-events/overview) From c4c5d7d7f524e73ca5c5ed9f25bcf88642c39353 Mon Sep 17 00:00:00 2001 From: Dimitar Nikolov Date: Tue, 22 Jul 2025 18:57:19 +0300 Subject: [PATCH 2/2] Update accessing-values-winforms-report-viewer.md --- ...accessing-values-winforms-report-viewer.md | 177 ++++++++++++------ 1 file changed, 124 insertions(+), 53 deletions(-) diff --git a/knowledge-base/accessing-values-winforms-report-viewer.md b/knowledge-base/accessing-values-winforms-report-viewer.md index c9d100ec9..35577082a 100644 --- a/knowledge-base/accessing-values-winforms-report-viewer.md +++ b/knowledge-base/accessing-values-winforms-report-viewer.md @@ -1,6 +1,6 @@ --- title: Accessing Data from Rendered Report in WinForms Viewer -description: Learn how to access values from a rendered TRDP or TRDX report in the WinForms Report Viewer by using report parameters or report events. +description: "Learn how to access values from a rendered TRDP or TRDX report in the WinForms Report Viewer by using report parameters or report events." type: how-to page_title: Extract Values from WinForms Report Viewer Processed Report meta_title: Extract Values from WinForms Report Viewer Processed Report @@ -11,22 +11,19 @@ ticketid: 1693205 --- ## Environment + - - - - - - - - - - + + + + + +
ProductReporting
Version19.1.25.521
ProductReporting
## Description -I need to display a TRDP or TRDX report in the WinForms Report Viewer, click a button, and extract specific values (e.g., client email) from the rendered report. Directly casting a `UriReportSource` to an `InstanceReportSource` results in an invalid cast error. Additionally, I need access to the processed report or the report's data source fields. +I need to display a TRDP or TRDX report in the **WinForms Report Viewer**, click a button, and extract specific values (e.g., client email) from the rendered report. Essentially, I need access to the processed report or the report's data source fields from outside the report. This knowledge base article also answers the following questions: - How to get data from a rendered report in WinForms Report Viewer? @@ -37,46 +34,96 @@ This knowledge base article also answers the following questions: ### Using Report Parameters -Use report parameters to retrieve values from the report. Connect the report parameter to the desired field in the report data source. +Use [report parameters]({%slug telerikreporting/designing-reports/connecting-to-data/report-parameters/overview%}) to retrieve values from the report. Connect the report parameter to the desired field of the [data source component]({%slug telerikreporting/designing-reports/connecting-to-data/data-source-components/overview%}) whose data you need to access. + +1. [Create a report parameter]({%slug telerikreporting/designing-reports/connecting-to-data/report-parameters/how-to-add-report-parameters%}) in the report designer, connect it to the needed [data source component]({%slug telerikreporting/designing-reports/connecting-to-data/data-source-components/overview%}), and bind its [Value](/api/telerik.reporting.reportparameter#Telerik_Reporting_ReportParameter_Value) and [ValueMember](/api/telerik.reporting.reportparameteravailablevalues#Telerik_Reporting_ReportParameterAvailableValues_ValueMember) properties to the required field. You may use the same expression for both properties - `= Fields.MyField`. +1. Access the report parameter value in the application code through the viewer's [ReportSource](/api/telerik.reportviewer.winforms.reportviewerbase#Telerik_ReportViewer_WinForms_ReportViewerBase_ReportSource) property: -1. Set up a report parameter in the report designer and bind its value to the required field: - ``` - = Fields.MyField - ``` -2. Access the report parameter value in your application code: - ```vb +````VB Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click - Dim reportParameterValue = Me.TKReportViewer.ReportSource.Parameters("MyParameter").Value + Dim reportParameterValue = Me.ReportViewer1.ReportSource.Parameters("MyParameter").Value End Sub - ``` +```` +````C# +private void Button1_Click(object sender, EventArgs e) +{ + var reportParameterValue = this.ReportViewer1.ReportSource.Parameters["MyParameter"].Value; +} +```` -This approach is simple and clean for retrieving individual values from the report. ### Using Report Events -For scenarios requiring multiple values from the processed report, use report events: - -1. Unpackage the TRDP file and assign custom event handlers to the report: - ```vb - Dim uri As String = "C:\Path\To\Report.trdp" - Dim reportPackager As New ReportPackager() - Dim instanceReportSource As New InstanceReportSource() - - Using sourceStream As FileStream = File.OpenRead(uri) - Dim report As Report = CType(reportPackager.UnpackageDocument(sourceStream), Report) - Dim table As Table = CType(report.Items.Find("table1", True)(0), Table) - Dim detailSection As DetailSection = CType(report.Items.Find("detailSection1", True)(0), DetailSection) - - AddHandler report.ItemDataBound, AddressOf Report_ItemDataBound - AddHandler table.ItemDataBound, AddressOf Table_ItemDataBound - - instanceReportSource.ReportDocument = report - End Using - ``` - -2. Define the event handler to process and retrieve data: - ```vb +For scenarios requiring multiple values from the processed report, use [report events]({%slug telerikreporting/using-reports-in-applications/program-the-report-definition/report-events/overview%}). For TRDP/TRDX files, they must be unpackaged/deserialized in order to assign custom event handlers to the reports. + +````C# + private void MainForm_Load(object sender, System.EventArgs e) + { + var uri = "C:\Path\To\Report.trdp"; + var reportPackager = new ReportPackager(); + var instanceReportSource = new InstanceReportSource(); + + using (var sourceStream = System.IO.File.OpenRead(uri)) + { + var report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream); + + // reading values from the detail section + var table = report.Items.Find("table1", true)[0] as Telerik.Reporting.Table; + table.ItemDataBound += Table_ItemDataBound; + + // reading values from a table + var detailSection = report.Items.Find("detailSection1", true)[0] as Telerik.Reporting.DetailSection; + report.ItemDataBound += Report_ItemDataBound; + + instanceReportSource.ReportDocument = report; + } + + this.reportViewer1.ReportSource = instanceReportSource; + this.reportViewer1.RefreshReport(); + } +```` +````VB +Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load + Dim uri As String = "C:\Path\To\Report.trdp" + Dim reportPackager As New ReportPackager() + Dim instanceReportSource As New InstanceReportSource() + + Using sourceStream As FileStream = File.OpenRead(uri) + Dim report As Report = CType(reportPackager.UnpackageDocument(sourceStream), Report) + + ' reading values from the detail section + Dim table As Table = CType(report.Items.Find("table1", True)(0), Table) + AddHandler table.ItemDataBound, AddressOf Table_ItemDataBound + + ' reading values from a table + Dim detailSection As DetailSection = CType(report.Items.Find("detailSection1", True)(0), DetailSection) + AddHandler report.ItemDataBound, AddressOf Report_ItemDataBound + + instanceReportSource.ReportDocument = report + End Using + + Me.reportViewer1.ReportSource = instanceReportSource + Me.reportViewer1.RefreshReport() +End Sub +```` + + +- Accessing data from a table + +````C# + private void Table_ItemDataBound(object sender, System.EventArgs e) + { + var processsingTable = sender as Telerik.Reporting.Processing.Table; + foreach (var row in processsingTable.Rows) + { + var firstCell = row.GetCell(0); + var textBoxItem = (Telerik.Reporting.Processing.TextBox)firstCell.Item; + Trace.WriteLine($"Row: {row.Index}, Cell: {firstCell.RowIndex}, Text: {textBoxItem.Value}"); + } + } +```` +````VB Private Sub Table_ItemDataBound(sender As Object, e As EventArgs) Dim processingTable As Telerik.Reporting.Processing.Table = CType(sender, Telerik.Reporting.Processing.Table) For Each row As Telerik.Reporting.Processing.TableRow In processingTable.Rows @@ -85,16 +132,40 @@ For scenarios requiring multiple values from the processed report, use report ev Trace.WriteLine(String.Format("Row: {0}, Cell: {1}, Text: {2}", row.Index, firstCell.RowIndex, textBoxItem.Value)) Next End Sub - ``` - -3. Use the event handlers to extract values from the report detail section or table. +```` + + +- Accessing data from a `textBox` item in the **detail** section of the report + +````C# + private void Report_ItemDataBound(object sender, System.EventArgs e) + { + var processingReport = sender as Telerik.Reporting.Processing.Report; + var group = ElementTreeHelper.GetChildByIndex(processingReport, 0); + + var detailSections = ElementTreeHelper.FindChildByName(group, "detailSection1", false); + foreach (var section in detailSections) + { + var textBoxJobTitle = (Telerik.Reporting.Processing.TextBox)ElementTreeHelper.GetChildByName(section, "textBox2"); + Trace.WriteLine($"Detail Section: {((Telerik.Reporting.Processing.DetailSection)section).Name}, Job Title: {textBoxJobTitle.Value}"); + } + } +```` +````VB +Private Sub Report_ItemDataBound(sender As Object, e As System.EventArgs) + Dim processingReport As Report = CType(sender, Report) + Dim group As Object = ElementTreeHelper.GetChildByIndex(processingReport, 0) + + Dim detailSections As IEnumerable(Of Object) = ElementTreeHelper.FindChildByName(group, "detailSection1", False) + For Each section As Object In detailSections + Dim textBoxJobTitle As TextBox = CType(ElementTreeHelper.GetChildByName(section, "textBox2"), TextBox) + Trace.WriteLine(String.Format("Detail Section: {0}, Job Title: {1}", CType(section, DetailSection).Name, textBoxJobTitle.Value)) + Next +End Sub +```` -This approach is suitable for retrieving multiple records or detailed data from the processed report. ## See Also -- [UriReportSource Documentation](https://docs.telerik.com/reporting/api/telerik.reporting.urireportsource) -- [InstanceReportSource Documentation](https://docs.telerik.com/reporting/api/telerik.reporting.instancereportsource) -- [Report Parameters Overview](https://docs.telerik.com/reporting/designing-reports/connecting-to-data/report-parameters/overview) -- [Unpackaging Report Definitions](https://docs.telerik.com/reporting/embedding-reports/program-the-report-definition/package-report-definition#unpackaging) -- [Report Events Overview](https://docs.telerik.com/reporting/embedding-reports/program-the-report-definition/report-events/overview) +* [Report Parameters Overview]({%slug telerikreporting/designing-reports/connecting-to-data/report-parameters/overview%}) +* [Report Events Overview]({%slug telerikreporting/using-reports-in-applications/program-the-report-definition/report-events/overview%})