From this post I'm going to describe how to bind dynamic data to an rdlc report. In here it loads data to the report based on selected value of a drop down. That means we can display different reports based on given parameters.
- First of all we will do the design of the web page (with a dropdown,a button and a reportviewer)
- Then add code behind methods as below.
[code] protected void btnview_Click(object sender, EventArgs e)
{
string id = ddlId.SelectedValue;
string filename = string.Empty;
string[] queries = null;
if (id == "1")
{
filename = "Test7-1";
string query1 = "select id,lat from Test3 where id=" + id + "";
string query2 = "select lng,colorId from Test3 where id=" + id + "";
queries = new string[2];
queries[0] = query1;
queries[1] = query2;
}
else if (id == "2")
{
filename = "Test7-2";
string query1 = "select id,lat from Test3 where id=" + id + "";
string query2 = "select lng,colorId from Test3 where id=" + id + "";
queries = new string[2];
queries[0] = query1;
queries[1] = query2;
}
runRptViewer(filename, queries);
ReportViewer1.Visible = true;
}
private void runRptViewer(string fileName,string[] queries)
{
DataTable[] datatables = fillDataTables(queries);
string reportpath = @"D:\Madara\Reports\" + fileName + ".rdl";
this.ReportViewer1.Reset();
this.ReportViewer1.LocalReport.ReportPath = reportpath;
getDataSetNames(datatables);
}
private DataTable[] fillDataTables(string[] queries)
{
int queryCount = queries.Length;
DataTable[] datatables = new DataTable[queryCount];
for (int i = 0; i < queryCount; i++)
{
DataTable dt = page6Service.getData(queries[i]);
datatables[i] = dt;
}
return datatables;
}
private void getDataSetNames(DataTable[] datatables)
{
ReportDataSourceCollection dataSources = ReportViewer1.LocalReport.DataSources;
this.ReportViewer1.LocalReport.DataSources.Clear();
IList
dataSetNames;
dataSetNames = this.ReportViewer1.LocalReport.GetDataSourceNames();
for (int i = 0; i < dataSetNames.Count; i++)
{
dataSources.Add(new ReportDataSource(dataSetNames[i], datatables[i]));
}
this.ReportViewer1.DataBind();
this.ReportViewer1.LocalReport.Refresh();
} [/code]
- Values of the sql data table.
- Result will be like this.