ReportView动态加载带参数的RDCL文件及子报表

时间:2022-12-20 08:07:14

本文来自Torres.Wu发表在博客园的博客,转载请标明出处。

同上一篇差不多,这次咱们加载带有子报表的RDCl文件。首先还是创建一个form程序,在form2窗体中添加一个ReporView控件,load方法如下:

private void Form2_Load(object sender, EventArgs e)
{
DataSet ds3 = new DataSet();
string fileName = System.Configuration.ConfigurationManager.AppSettings["file3"].ToString();
string rptFilePath = System.IO.Path.Combine(Application.StartupPath, fileName);
this.reportViewer1.LocalReport.ReportPath = rptFilePath; this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
try
{
ds3 = getDS3();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message);
}
Microsoft.Reporting.WinForms.ReportDataSource r3 = new Microsoft.Reporting.WinForms.ReportDataSource();
r3.Value = ds3.Tables[];
r3.Name = "DataSet1";
this.reportViewer1.LocalReport.DataSources.Add(r3);
//添加加载子报表事件
this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessingEventHandler);
this.reportViewer1.RefreshReport();
}

大家看到与上一篇不同的是这次在load事件中加了子报表的加载事件,此事件在处理子报表时发生。

同样的,要有主报表数据源和子报表数据源:

 //主报表数据源
DataSet getDS3()
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionSQL"].ToString();
SqlConnection conn = new SqlConnection(connStr);
//读取sql
XmlDocument xmldoc = new XmlDocument();
string fileName = System.Configuration.ConfigurationManager.AppSettings["file3"].ToString();
string rptFilePath = System.IO.Path.Combine(Application.StartupPath, fileName);
xmldoc.Load(rptFilePath);
//this.reportViewer1.LocalReport.ReportPath = rptFilePath; XmlNodeList sqlM = xmldoc.GetElementsByTagName("CommandText");
string sql = sqlM[].InnerXml.ToString();
XmlNodeList canshu = xmldoc.GetElementsByTagName("QueryParameter");
//获取数据源
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
List<string> list = new List<string>();
list.Add("生物科学系");
//如果有参数 传参数
if (canshu.Count > )
{
XmlNodeList canshuList = xmldoc.GetElementsByTagName("ReportParameter");
if (canshuList.Count > )
{
for (int i = ; i < canshuList.Count; i++)
{ da.SelectCommand.Parameters.Add("@" + canshuList[i].Attributes.GetNamedItem("Name").Value, list[i]); //参数传给数据源
ReportParameter rp = new ReportParameter(canshuList[i].Attributes.GetNamedItem("Name").Value, list[i]);
this.reportViewer1.LocalReport.SetParameters(rp); //参数传给报表 }
} }
DataSet de = new DataSet();
da.Fill(de, "Table");
conn.Close();
return de;
}
//子报表数据源
DataSet getDS4()
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionSQL"].ToString();
SqlConnection conn = new SqlConnection(connStr);
//读取sql
XmlDocument xmldoc = new XmlDocument();
string fileName = System.Configuration.ConfigurationManager.AppSettings["file4"].ToString();
string rptFilePath = System.IO.Path.Combine(Application.StartupPath, fileName);
xmldoc.Load(rptFilePath);
XmlNodeList sqlM = xmldoc.GetElementsByTagName("CommandText");
string sql = sqlM[].InnerXml.ToString();
XmlNodeList canshu = xmldoc.GetElementsByTagName("QueryParameter");
//获取数据源
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
List<string> list = new List<string>();
list.Add("生物科学系");
//如果有参数 传参数
if (canshu.Count > )
{
XmlNodeList canshuList = xmldoc.GetElementsByTagName("ReportParameter");
if (canshuList.Count > )
{
for (int i = ; i < canshuList.Count; i++)
{
da.SelectCommand.Parameters.Add("@" + canshuList[i].Attributes.GetNamedItem("Name").Value, list[i]); //参数传给数据源
}
}
}
DataSet de = new DataSet();
da.Fill(de, "Table");
conn.Close();
return de;
}

还有在处理子报表时发生的事件:

        /// <summary>
/// 为子报表加数据源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void SubreportProcessingEventHandler(object sender, SubreportProcessingEventArgs e)
{
DataSet ds4 = new DataSet();
try
{
ds4 = getDS4();
}
catch (Exception)
{ throw;
}
DataTable dt = ds4.Tables[]; Microsoft.Reporting.WinForms.ReportDataSource r3 = new Microsoft.Reporting.WinForms.ReportDataSource();
r3.Value = ds4.Tables[];
r3.Name = "DataSet1";
e.DataSources.Add(r3);
}本文来自Torres.Wu发表在博客园的博客,转载请标明出处。

这样就大功告成了,配置文件和上一篇一样。

本文来自Torres.Wu发表在博客园的博客,转载请标明出处。