简体   繁体   中英

How can I bind code-behind dataset to RDLC?

I have a page which contains a dropdownlist and reportviewer, and I use dropdownlist.selectValue as my parameter to query, and build a datatable I want

My question is: How to bind the code-behind datatable to RDLC and how to show on reportviewer

Do a lot of googling and find the closet result https://blogs.msdn.microsoft.com/sqlforum/2011/04/27/walkthrough-assign-dataset-dynamically-created-in-code-to-your-local-report-with-reportviewer/

Only one error

this.DataTable1BindingSource.DataSource = ds;
this.reportViewer1.RefreshReport();

I guess this exactly is where rdlc bind my coded datatable.

But I can't find DataTable1BindingSource

aspx

<asp:ScriptManager runat="server"></asp:ScriptManager> 
<rsweb:ReportViewer ID="ReportViewer1" runat="server" >
<LocalReport ReportPath="Report1.rdlc">
    <DataSources>
        <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" />
    </DataSources>
</LocalReport>
</rsweb:ReportViewer>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="DataSet1TableAdapters."></asp:ObjectDataSource>

cs

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{  
code dataset and datatable here
}

Expect rdlc bind my own coded datatable

Finally I worked out by making my coded dataset rename to the same name in the DataSet.xsd. Not only the schema, You need to make the dataset name in your code match with the name in xsd file.

For example: Create a DataSet1.xsd contains a datatable named DataTable1(default name)

and in your code is

DataSet DataSet1 = new DataSet("something");
DataTable DataTable1 = DataSet1.Tables.Add("something");

If you declare another name differ from the name defined in xsd, you will get an error on ReportViewer

A data source instance has not been supplied for the data source 'DataSet1'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM