查询reportViewer从表单中获取参数

时间:2012-09-06 01:42:03

标签: c# winforms reporting reportviewer

我很擅长使用ReportViewer。我有一个问题,我不知道如何解决。

我有一个日期TextBox和一个位置TextBox。我的问题是,当用户键入日期和位置时,如何在ReportViewer中显示相同的数据。以下是一些更好理解的截图。任何帮助将不胜感激。

如果需要更多详细信息,请与我们联系。

This is the report.rdlc for the reportviewer. The AllocationDate and the LocationName shown in the pic below will show the Date and Location when the user key in the data.

This is the Windows Form that has the Textboxes to key in the Date and Location

1 个答案:

答案 0 :(得分:0)

我假设您将在表单中添加ShowButton按钮。但是,您可以使用TextBox事件(例如,Focus Lost,Text Changed等)而不是按钮单击事件。

   private void ShowButton_Click(object sender, EventArgs e)
    {
        DateTime allocationDate = Convert.ToDateTime(allocDateTextBox.Text);
        string   locationName   = locationTextBox.Text;

        //You can create as many datasources matching the name of DataSet in your report
        ReportDataSource rds = new ReportDataSource("DataSet1");
        rds.Value = getData(String.Format("Select * from myTable where theDate={0} AND Location={1}", allocationDate, locationName));

        reportViewer1.LocalReport.DataSources.Clear();

        //add as many datasources created above
        reportViewer1.LocalReport.DataSources.Add(rds);
        reportViewer1.RefreshReport();
    }

   private DataTable getData(string query)
   {
      //Select new record from database based on the new query with the new criteria
      //return the record as DataTable, DataSet or Collection of object
   }