本文来自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发表在博客园的博客,转载请标明出处。

ReportView动态加载带参数的RDCL文件及子报表的更多相关文章

  1. ReportView动态加载带参数的RDCL文件

    在vs里新建一个winform程序"ReportViewTest",在form1中添加一个reportView控件,from1的load事件如下: private void For ...

  2. 为不同分辨率单独做样式文件,在页面头部用js判断分辨率后动态加载定义好的样式文件

    为不同分辨率单独做样式文件,在页面头部用js判断分辨率后动态加载定义好的样式文件.样式文件命名格式如:forms[_屏幕宽度].css,样式文件中只需重新定义文本框和下拉框的宽度即可. 在包含的头文件 ...

  3. js实用方法记录-js动态加载css、js脚本文件

    js实用方法记录-动态加载css/js 附送一个加载iframe,h5打开app代码 1. 动态加载js文件到head标签并执行回调 方法调用:dynamicLoadJs('http://www.yi ...

  4. JavaScript 之 动态加载JS代码或JS文件

    2.动态加载JS文件 <script type="text/javascript"> function loadScript(url, callback) { var ...

  5. 使用js加载器动态加载外部js、css文件

    let MiniSite = new Object(); /** * 判断浏览器 */ MiniSite.Browser = { ie: /msie/.test(window.navigator.us ...

  6. 动态加载JS(css)文件

    <script language="javascript">document.write("<script src='test.js'><\ ...

  7. CxImage动态加载图片(判断图片文件类型)

    1.打开一张图可以通过创建一个新的CxImage对象来完成,通过构造函数来打开一张图CxImage::CxImage(const char * filename, DWORD imagetype)其中 ...

  8. 动态加载、移除css文件

    修改样式有通过修改class属性来更改,也可以通过动态引入外部的css文件来改变对应的样式展示. 这里就介绍动态引入.移除css文件 ///添加平板样式文件 function loadStyles() ...

  9. C#,动态加载DLL,通过反射,调用参数,方法,窗体

    .net中常会用到动态加载DLL,而DLL中可能包含各种参数.方法.窗体,如何来调用动态加载这些参数.方法.窗体呢? 在C#中,我们要使用反射,首先要搞清楚以下命名空间中几个类的关系: System. ...

随机推荐

  1. 2017 .NET 開發者須知

    筆記-Scott Hanselman 的 2017 .NET 開發者須知 转载http://blog.darkthread.net/post-2017-01-16-dotnet-dev-should- ...

  2. Eclipse设置代码自动提示

    Eclipse只需几步简单的设置就可以像idea那样代码自动提示了,喜欢的小伙伴可以赶紧动手设置,提升效率. 第一步:打开Eclipse --> Window --> Preference ...

  3. c++ singleton单例模式

    方法1:加锁的经典懒汉实现: class singleton { public: static pthread_mutex_t mutex; static singleton* initance(); ...

  4. jsp页面取值

    一般就用el表达式 ${recordList[4].baseRate8.split("/")[0] } <s:date name="recordList[#id]. ...

  5. JDBC(四)

    1 Apache DBUtils框架 1.1 DBUtils简介 commons-dbutils是Apache组织提供的一个开源JDBC工具类库,它是对JDBC的简单封装,学习成本非常低,并且使用db ...

  6. python _init_学习

    今天继续学习python,接触了_init_,感觉很好玩照着教程手写了一些代码,感觉编程语言是互通的,只是换个了形式来表达 #coding=utf-8#类似于java的构造器class Person: ...

  7. 04_VMware虚拟机网络配置

    占位占位占位占位占位占位占位占位

  8. 不依赖jstack的java 线程dump和死锁检查工具

    java线程dump可以使用jdk的命令"jstack  pid"完成,死锁检查可以用jconsole查看到.这两个工具是java调试的常用方法. 我遇到的问题是:在sles11s ...

  9. centos 6.3安装ssh

    centos 6.3安装ssh   安装ssh服务器端软件 yum install openssh-server   安装ssh客户端软件   yum install openssh-clients ...

  10. iOS-NSPredicate正则验证【三种验证方法】

    1.NSPredicate验证(谓词匹配) ///验证(string:验证的字符串) + (BOOL)stringValidate:(NSString *)string{ NSString *regu ...