窗体frmReport是一个报表窗体,用于预览RDLC报表,第一次加载是速度很慢,如果用户在凤有加载完成前就点击关闭的话,就会提示"在未创建句柄之前,不能调用控件的Invoke和BeginInvoke",好几天了都没有找到解决办法?
具体代码如:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 祥瑞JXC
{
public partial class frmReport : Form
{
public frmReport()
{
InitializeComponent();
this.Text = "采购单预览";
}
private void frmReport_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
}
string rdlcFullPath;
/// <summary>
/// 打印报表
/// </summary>
/// <param name="rdlcPath">报表的路径</param>
/// <param name="dataSourceName">数据源名称(报表设计里写的)</param>
/// <param name="dataSourceValue">数据源表名</param>
/// <param name="privew">是否直接打印</param>
public void frmShow(string rdlcPath, string dataSourceName, DataTable dataSourceValue)
{
Microsoft.Reporting.WinForms.ReportDataSource repDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
if (rdlcFullPath!=rdlcPath)
{
this.reportViewer1.LocalReport.ReportPath = rdlcPath;
rdlcFullPath = rdlcPath;
}
repDataSource.Name = dataSourceName;
repDataSource.Value = dataSourceValue;
this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(repDataSource);
this.reportViewer1.LocalReport.Refresh();
// this.ShowDialog();
}
}
}
调用代码: private frmReport frmreport = null;
frmreport = new frmReport();
frmreport.frmShow(Application.StartupPath + @"\report\CGD.rdlc", "report_cgd", dt);
具体代码如:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 祥瑞JXC
{
public partial class frmReport : Form
{
public frmReport()
{
InitializeComponent();
this.Text = "采购单预览";
}
private void frmReport_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
}
string rdlcFullPath;
/// <summary>
/// 打印报表
/// </summary>
/// <param name="rdlcPath">报表的路径</param>
/// <param name="dataSourceName">数据源名称(报表设计里写的)</param>
/// <param name="dataSourceValue">数据源表名</param>
/// <param name="privew">是否直接打印</param>
public void frmShow(string rdlcPath, string dataSourceName, DataTable dataSourceValue)
{
Microsoft.Reporting.WinForms.ReportDataSource repDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
if (rdlcFullPath!=rdlcPath)
{
this.reportViewer1.LocalReport.ReportPath = rdlcPath;
rdlcFullPath = rdlcPath;
}
repDataSource.Name = dataSourceName;
repDataSource.Value = dataSourceValue;
this.reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(repDataSource);
this.reportViewer1.LocalReport.Refresh();
// this.ShowDialog();
}
}
}
调用代码: private frmReport frmreport = null;
frmreport = new frmReport();
frmreport.frmShow(Application.StartupPath + @"\report\CGD.rdlc", "report_cgd", dt);
{
this.reportViewer1.RefreshReport();
}因为刷新报表会有点慢,可能MS用的是多线程过程的,子线程刷新完成后,主线程已关闭了,还有就是this.ShowDialog();这个是每次关闭不是直正的DISPOSE,
开始试过一种办法,就是设置窗体边框不可见,在报表刷新完成就才显示关闭按钮,但this.reportViewer1.RenderingComplete这个事件好像也达不到目的,
期待高手
this.reportViewer1.RefreshReport(); 最近看到ms很多高级的编程都不是用的load事件 而是用actived事件。你换这个事件,试一下。