无法在C#中将数据集分配给ReportDataSource

时间:2011-04-15 14:42:39

标签: c# winforms reporting-services report

我正在尝试将数据集作为报表的数据源传递,我希望借助在winform的表单上托管的Microsft Reporting Control来查看该报表。我为此目的使用以下代码,但未能完成任务。

    private void Form1_Load(object sender, EventArgs e)
    {
        // Sql Connection Object
        SqlConnection con = new SqlConnection(@"Data Source=SEVEN01-PC\SQLEXPRESS;Initial Catalog=RealWorld;Integrated Security=SSPI;");

        // Sql Command Object
        SqlCommand cmd = new SqlCommand("Select * from ProductReorder", con);

        try
        {
            // Open Connection
            con.Open();

            // Dataset Object
            DataSet ds = new DataSet();

            // Sql DataReader Object
            SqlDataReader reader = cmd.ExecuteReader();

            // Fill Data Set
            ds.Tables[0].Load(reader);

            // Close Sql Datareader and Connection Objects
            reader.Close();
            con.Close();

            //provide local report information to viewer
            reportViewer1.LocalReport.ReportEmbeddedResource = "ProductReorder.rptProductReorder.rdlc";

            //prepare report data source
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "dsProductReorder_dtProductReorder";
            rds.Value = ds.Tables[0];
            reportViewer1.LocalReport.DataSources.Add(rds);

            //load report viewer
            this.reportViewer1.RefreshReport();
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }

如何将数据集分配给报表数据源的任何建议!

2 个答案:

答案 0 :(得分:2)

也许你可以试试这个......

    LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("dsProductReorder_dtProductReorder", d.Tables[0]));

答案 1 :(得分:0)

ReportTableAdapter ta = new ReportTableAdapter();
var ds= ra.GetData();
ReportDataSource rd = new ReportDataSource("RepotrDS",ds.ToList());

这适合我。