我正在尝试在 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;
}
但是,如果我通过填充数据库中的列对函数进行轻微更改以使我的数据表真正动态化,那么我的函数将不会显示在报表向导中。
这是我改变的功能
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;
}
您会看到,我对该函数所做的唯一更改是从数据库中查询并在循环中生成报表数据集列,该循环遍历数据库数据。但是由于这种变化,我的功能没有出现在报告向导中。如果我省略代码,它会再次出现。
我可以使用此函数很好地生成 GridView,但问题在于 RDLC 报告。
我的目标是使用数据库结果生成报告数据表。请帮我。