Shu*_*aib 5 c# asp.net rdlc reporting-services dynamic-rdlc-generation
我正在尝试在 ASP.NET 中生成 RDLC 报告,其中数据集的列将是动态的并且仅在运行时确定。
我创建了一个返回 DataTable 的函数,通过在 RDLC 报告向导中选择此函数,我可以成功生成报告。
public DataTable GetTable()
{
// Here we create a DataTable with four columns.
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Columns.Add("testColumn", typeof(DateTime));
// Here we add five DataRows.
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}
Run Code Online (Sandbox Code Playgroud)


但是,如果我通过填充数据库中的列对函数进行轻微更改,使我的数据表真正动态,则我的函数不会显示在报告向导中。
这是我改变的功能
public DataTable GetTable2()
{
// Here we create a DataTable with four columns.
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Columns.Add("testColumn", typeof(DateTime));
SqlConnection connection = new SqlConnection();
connection = Connection.getConnection();
connection.Open();
string tableName = "";
tableName += "Subject";
string Query = "select * from " + tableName + " where Status = 0;";
SqlDataAdapter da = new SqlDataAdapter(Query, connection);
DataSet ds = new DataSet();
da.Fill(ds);
DataRowCollection collection = ds.Tables[0].Rows;
foreach (DataRow row in collection)
{
// Here we add five DataRows.
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
}
connection.Close();
return table;
}
Run Code Online (Sandbox Code Playgroud)
您会看到,我对该函数所做的唯一更改是从数据库查询并在迭代数据库数据的循环中生成报表数据集列。但由于这一更改,我的函数没有显示在报表向导中。如果我省略代码,它会再次显示。
我可以使用此函数很好地生成 GridView,但问题在于 RDLC 报告。
我的目标是使用数据库结果生成报告数据表。请帮我。
小智 1
我不确定你的问题是否表述得太优雅。创建真正的临时报告是不可能的。解决方法是这样的。- 创建 rdlc 文件 - 以您从中获取数据的格式创建数据集 xsd 文件。您必须手动执行此操作,并且每一列都应与您的最终输出匹配 - 将 rdlc 文件绑定到此数据集数据源 - 设计您的报告
现在在查询生成部分,您必须将动态数据绑定到 rdlc,如下所示
Dim repDS As New Microsoft.Reporting.WebForms.ReportDataSource
repDS.Value = dT
repDS.Name = "dsConsolReq_dt1" 'hard coded here, but you should get it dynamically perhaps from the querystring or the db
RV1.LocalReport.DataSources.Clear()
RV1.LocalReport.DataSources.Add(repDS)
RV1.LocalReport.ReportPath = "Reports\rep_itemr1.rdlc" 'same again, use dynamic values
RV1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local
RV1.LocalReport.Refresh()Run Code Online (Sandbox Code Playgroud)
这里的关键是报告数据源的名称。它应该与您为设计报告而创建的虚拟数据集/数据表完全匹配