原文
场景:从数据读取数据集,在RDLC报表控件中展现。其中一字段保存的是图片的虚拟路径,图片源文件保存在服务器的固定文件夹中。
重点:在RDLC中显示图片必须满足两个条件,才能执行正常。处理方式略过,
1、图片完整的绝对路径。
2、完整绝对路径前必须加上 “file:\”; 例如:file:\G:\Project\Upload\Images\aaa.jpg
建议:直接在SQL语句中拼接完整绝对路径。
RDLC图片控件属性设置:
1、在报表适合的位置中放Image控件,并按照下图设置。选择“外部”,并选择数据库字段。
2、代码中设置属性。
this.ReportViewer1.LocalReport.EnableExternalImages = true;
完整代码:
DataTable dtSource = new AttendanceBLL().GetUserAttendanceListByYearMonth(GetParameter());
this.ReportViewer1.Visible = false;
lblText.Visible = false;
if (dtSource.Rows.Count > 0)
{
string reportPath = "";
string reportSource = "";
reportPath = "Reports/UsersAttendance.rdlc";
reportSource = "Attendance";
this.ReportViewer1.LocalReport.DataSources.Clear();
lblText.Visible = false;
this.ReportViewer1.LocalReport.ReportPath = reportPath;
this.ReportViewer1.LocalReport.EnableExternalImages = true;
ReportDataSource rds = new ReportDataSource(reportSource, dtSource);
this.ReportViewer1.LocalReport.DataSources.Add(rds);
this.ReportViewer1.LocalReport.Refresh();
this.ReportViewer1.Visible = true;
}
else
{
this.ReportViewer1.Visible = false;
lblText.Visible = true;
}
效果: