过滤 ReportViewer 数据?

filter ReportViewer data?

我正在尝试创建一个按钮来按名称加载和过滤我的 ReportViewer,但输出是未提供的数据源实例。这是我的代码:

private void btnReport_Click(object sender, EventArgs e)
        {
            String sql = "Select * from practise Where name ='" + textBox1.Text + "'";
            SqlConnection con = new SqlConnection(ConnectionString);
            SqlDataAdapter adp = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            ReportDataSource rds = new ReportDataSource("practise", ds.Tables[0]);
            reportViewer2.ProcessingMode = ProcessingMode.Local;
            reportViewer2.LocalReport.ReportPath = "Report1.rdlc";
            if (ds.Tables[0].Rows.Count > 0)
            {
                reportViewer2.LocalReport.DataSources.Clear();
                reportViewer2.LocalReport.DataSources.Add(rds);
                reportViewer2.RefreshReport();
            }

这可能很简单。请检查报告中的数据集名称是否默认不是“DataSet1”。另外,确保报告的路径是正确的。这是一个工作示例:

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DemoDB"].ConnectionString))
            {
                 String sql = "Select * from practise Where name ='" + textBox1.Text + "'";
                SqlDataAdapter adp = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                ReportDataSource rds = new ReportDataSource("DataSet1", ds.Tables[0]);
                reportViewer2.ProcessingMode = ProcessingMode.Local;
              reportViewer2.LocalReport.ReportPath = "Report1.rdlc";
                if (ds.Tables[0].Rows.Count > 0)
                {
                    reportViewer2.LocalReport.DataSources.Clear();
                    reportViewer2.LocalReport.DataSources.Add(rds);
                    reportViewer2.RefreshReport();
                }
            }

我希望这会对某人有所帮助。