开发者

VB.NET / Windows Forms - Data from user input to DataReport?

I'm developing a windows forms application using VB.NET. I'm currently working on DataReport (by Microsoft, not Crystal). Everything works fine when using DataSet as DataSource. But I have a question on how am I going to put data to my DataReport from a user input? Preferably when the user is on the act of printing(clicking the print button in the ReportViewer开发者_C百科), a Form will pop-up and ask him for input. That input I'm talking about is random names of people, and we don't have to track their names so there's no reason to put it on the database.

Here's my simple setup:

  • MainForm.vb (Form)

    • MyReportViewer (ReportViewer)
  • MyReport.rdlc (DataReport)

  • InputForm (Form)

    • MyInputBox (TextBox)

If it is impossible to do it on the act of printing. Maybe on the MainForm_Load event or before the generation of report can do. I've searched the net for an hour but I'm out of luck.


I've found the answer, it's called ReportParameters. Here's how to set it:

'Get input from user...
Dim OneParameter As String = InputBox("Enter something to display on report!", "Report Parameter")

'Prepare report parameters... The `SetParameters` requires array of `ReportParameter`...
Dim ReportParameters As New List(Of ReportParameter)
'Add the user input to `ReportParameter` array...
ReportParameters.Add(New ReportParameter("OneParameter", OneParameter, True))

'Set the `ReportParameters` of the `ReportViewer`...
FormWithReportViewer.TheReportViewer.LocalReport.SetParameters(ReportParameters)
FormWithReportViewer.Show()
FormWithReportViewer.Focus()

On the DataReport(.rdlc), we have to add a ReportParameters (Menu>Report>Report Parameters). The same name (OneParameter) should be used to avoid error. We can now use the parameter in our textbox, just put =Parameters!OneParameter.Value and we're done!

Note: We have to set the report parameters before rendering the report. If we cannot avoid that, refresh the report after adding parameters so it will be updated:

FormWithReportViewer.TheReportViewer.RefreshReport
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜