C# 将数据表绑定到 rdlc 报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18056981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:10:21  来源:igfitidea点击:

Binding datatable to an rdlc report

c#asp.netdatatablerdlc

提问by Ankur

I have used Data set to bind my rdlc reports using my Stored Procedure. Can I bind a manipulated data table to my rdlc report instead of Data set(.xsd).

我已经使用数据集使用我的存储过程来绑定我的 rdlc 报告。我可以将操纵的数据表绑定到我的 rdlc 报告而不是数据集 (.xsd)。

采纳答案by meda

Your reportViewer on ASPX

您在 ASPX 上的报表查看器

<rsweb:ReportViewer ID="ReportViewer1" runat="server" SizeToReportContent="True">
</rsweb:ReportViewer>

Method to get data set

获取数据集的方法

private DataTable GetSPResult()
{
    DataTable ResultsTable = new DataTable();

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);

    try
    {
        SqlCommand cmd = new SqlCommand("yourStorePorcedure", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", 1);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ResultsTable);
    }

    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
    }

    return ResultsTable;
}

Bind result to reportviewer

将结果绑定到报表查看器

DataTable dt = GetSPResult();
ReportViewer1.Visible = true;
ReportViewer1.LocalReport.ReportPath = "Report1.rdlc";
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));

Add DataSet to your Project, then add a tableadapter to it:

将 DataSet 添加到您的项目,然后向其中添加一个表适配器:

enter image description here

在此处输入图片说明

Go through the wizard and create the entity using the store procedure

通过向导并使用存储过程创建实体

Then Make sure the DataSet file is added to your rdlc

然后确保将 DataSet 文件添加到您的 rdlc

enter image description here

在此处输入图片说明

Now you need to design , and add the fields or it will be blank

现在您需要设计,并添加字段,否则它将为空白

回答by RAJESH KUMAR

Yes we can use manipulated datatable as datasource to report.but the field name should be same.

是的,我们可以使用操纵数据表作为数据源来报告。但字段名称应该相同。

DataTable   dtReportData="yourdata source"

YourReportViewer.LocalReport.DataSources.Clear();
ReportDataSource RDS1 = new ReportDataSource("SampleReport", dtReportData);
YourReportViewer.ProcessingMode = ProcessingMode.Local;
YourReportViewer.LocalReport.EnableExternalImages = true;
YourReportViewer.LocalReport.ReportEmbeddedResource = "Your Report Path";
YourReportViewer.LocalReport.DataSources.Add(RDS1);