|
| 1 | +--- |
| 2 | +title: Accessing Data from Rendered Report in WinForms Viewer |
| 3 | +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." |
| 4 | +type: how-to |
| 5 | +page_title: Extract Values from WinForms Report Viewer Processed Report |
| 6 | +meta_title: Extract Values from WinForms Report Viewer Processed Report |
| 7 | +slug: accessing-values-winforms-report-viewer |
| 8 | +tags: reporting, winforms, report viewer, parameters, report items |
| 9 | +res_type: kb |
| 10 | +ticketid: 1693205 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tbody> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>Reporting</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +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. |
| 27 | + |
| 28 | +This knowledge base article also answers the following questions: |
| 29 | +- How to get data from a rendered report in WinForms Report Viewer? |
| 30 | +- How to use report parameters to retrieve values from a report? |
| 31 | +- How to use report events to access processed report values? |
| 32 | + |
| 33 | +## Solution |
| 34 | + |
| 35 | +### Using Report Parameters |
| 36 | + |
| 37 | +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. |
| 38 | + |
| 39 | +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`. |
| 40 | +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: |
| 41 | + |
| 42 | + |
| 43 | +````VB |
| 44 | + Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click |
| 45 | + Dim reportParameterValue = Me.ReportViewer1.ReportSource.Parameters("MyParameter").Value |
| 46 | + End Sub |
| 47 | +```` |
| 48 | +````C# |
| 49 | +private void Button1_Click(object sender, EventArgs e) |
| 50 | +{ |
| 51 | + var reportParameterValue = this.ReportViewer1.ReportSource.Parameters["MyParameter"].Value; |
| 52 | +} |
| 53 | +```` |
| 54 | + |
| 55 | + |
| 56 | +### Using Report Events |
| 57 | + |
| 58 | +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. |
| 59 | + |
| 60 | +````C# |
| 61 | + private void MainForm_Load(object sender, System.EventArgs e) |
| 62 | + { |
| 63 | + var uri = "C:\Path\To\Report.trdp"; |
| 64 | + var reportPackager = new ReportPackager(); |
| 65 | + var instanceReportSource = new InstanceReportSource(); |
| 66 | + |
| 67 | + using (var sourceStream = System.IO.File.OpenRead(uri)) |
| 68 | + { |
| 69 | + var report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream); |
| 70 | + |
| 71 | + // reading values from the detail section |
| 72 | + var table = report.Items.Find("table1", true)[0] as Telerik.Reporting.Table; |
| 73 | + table.ItemDataBound += Table_ItemDataBound; |
| 74 | + |
| 75 | + // reading values from a table |
| 76 | + var detailSection = report.Items.Find("detailSection1", true)[0] as Telerik.Reporting.DetailSection; |
| 77 | + report.ItemDataBound += Report_ItemDataBound; |
| 78 | + |
| 79 | + instanceReportSource.ReportDocument = report; |
| 80 | + } |
| 81 | + |
| 82 | + this.reportViewer1.ReportSource = instanceReportSource; |
| 83 | + this.reportViewer1.RefreshReport(); |
| 84 | + } |
| 85 | +```` |
| 86 | +````VB |
| 87 | +Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load |
| 88 | + Dim uri As String = "C:\Path\To\Report.trdp" |
| 89 | + Dim reportPackager As New ReportPackager() |
| 90 | + Dim instanceReportSource As New InstanceReportSource() |
| 91 | + |
| 92 | + Using sourceStream As FileStream = File.OpenRead(uri) |
| 93 | + Dim report As Report = CType(reportPackager.UnpackageDocument(sourceStream), Report) |
| 94 | + |
| 95 | + ' reading values from the detail section |
| 96 | + Dim table As Table = CType(report.Items.Find("table1", True)(0), Table) |
| 97 | + AddHandler table.ItemDataBound, AddressOf Table_ItemDataBound |
| 98 | + |
| 99 | + ' reading values from a table |
| 100 | + Dim detailSection As DetailSection = CType(report.Items.Find("detailSection1", True)(0), DetailSection) |
| 101 | + AddHandler report.ItemDataBound, AddressOf Report_ItemDataBound |
| 102 | + |
| 103 | + instanceReportSource.ReportDocument = report |
| 104 | + End Using |
| 105 | + |
| 106 | + Me.reportViewer1.ReportSource = instanceReportSource |
| 107 | + Me.reportViewer1.RefreshReport() |
| 108 | +End Sub |
| 109 | +```` |
| 110 | + |
| 111 | + |
| 112 | +- Accessing data from a table |
| 113 | + |
| 114 | +````C# |
| 115 | + private void Table_ItemDataBound(object sender, System.EventArgs e) |
| 116 | + { |
| 117 | + var processsingTable = sender as Telerik.Reporting.Processing.Table; |
| 118 | + foreach (var row in processsingTable.Rows) |
| 119 | + { |
| 120 | + var firstCell = row.GetCell(0); |
| 121 | + var textBoxItem = (Telerik.Reporting.Processing.TextBox)firstCell.Item; |
| 122 | + Trace.WriteLine($"Row: {row.Index}, Cell: {firstCell.RowIndex}, Text: {textBoxItem.Value}"); |
| 123 | + } |
| 124 | + } |
| 125 | +```` |
| 126 | +````VB |
| 127 | + Private Sub Table_ItemDataBound(sender As Object, e As EventArgs) |
| 128 | + Dim processingTable As Telerik.Reporting.Processing.Table = CType(sender, Telerik.Reporting.Processing.Table) |
| 129 | + For Each row As Telerik.Reporting.Processing.TableRow In processingTable.Rows |
| 130 | + Dim firstCell As Telerik.Reporting.Processing.TableCell = row.GetCell(0) |
| 131 | + Dim textBoxItem As Telerik.Reporting.Processing.TextBox = CType(firstCell.Item, Telerik.Reporting.Processing.TextBox) |
| 132 | + Trace.WriteLine(String.Format("Row: {0}, Cell: {1}, Text: {2}", row.Index, firstCell.RowIndex, textBoxItem.Value)) |
| 133 | + Next |
| 134 | + End Sub |
| 135 | +```` |
| 136 | + |
| 137 | + |
| 138 | +- Accessing data from a `textBox` item in the **detail** section of the report |
| 139 | + |
| 140 | +````C# |
| 141 | + private void Report_ItemDataBound(object sender, System.EventArgs e) |
| 142 | + { |
| 143 | + var processingReport = sender as Telerik.Reporting.Processing.Report; |
| 144 | + var group = ElementTreeHelper.GetChildByIndex(processingReport, 0); |
| 145 | + |
| 146 | + var detailSections = ElementTreeHelper.FindChildByName(group, "detailSection1", false); |
| 147 | + foreach (var section in detailSections) |
| 148 | + { |
| 149 | + var textBoxJobTitle = (Telerik.Reporting.Processing.TextBox)ElementTreeHelper.GetChildByName(section, "textBox2"); |
| 150 | + Trace.WriteLine($"Detail Section: {((Telerik.Reporting.Processing.DetailSection)section).Name}, Job Title: {textBoxJobTitle.Value}"); |
| 151 | + } |
| 152 | + } |
| 153 | +```` |
| 154 | +````VB |
| 155 | +Private Sub Report_ItemDataBound(sender As Object, e As System.EventArgs) |
| 156 | + Dim processingReport As Report = CType(sender, Report) |
| 157 | + Dim group As Object = ElementTreeHelper.GetChildByIndex(processingReport, 0) |
| 158 | + |
| 159 | + Dim detailSections As IEnumerable(Of Object) = ElementTreeHelper.FindChildByName(group, "detailSection1", False) |
| 160 | + For Each section As Object In detailSections |
| 161 | + Dim textBoxJobTitle As TextBox = CType(ElementTreeHelper.GetChildByName(section, "textBox2"), TextBox) |
| 162 | + Trace.WriteLine(String.Format("Detail Section: {0}, Job Title: {1}", CType(section, DetailSection).Name, textBoxJobTitle.Value)) |
| 163 | + Next |
| 164 | +End Sub |
| 165 | +```` |
| 166 | + |
| 167 | + |
| 168 | +## See Also |
| 169 | + |
| 170 | +* [Report Parameters Overview]({%slug telerikreporting/designing-reports/connecting-to-data/report-parameters/overview%}) |
| 171 | +* [Report Events Overview]({%slug telerikreporting/using-reports-in-applications/program-the-report-definition/report-events/overview%}) |
0 commit comments