C# – RDLC dynamically add images to report

asp.netcrdlc

I have list of images in remote server, i need to show some of these images in a existing report. I tried by dropping an image control to the rdlc and it works for single image.

But i need to show more than one image, which has no fixed count.

I referred below posts, but none are clear:

C# – Add List of images to report viewer

Insert unknown number of Images to the report at runtime using reportviewer

http://www.c-sharpcorner.com/blogs/6194/display-image-in-rdlc-report-microsoft-report-viewer.aspx%27

I know there might be possible duplicates for this request, please guide me if so.

Best Answer

I started by adding a table to the existing reports, as in above mentioned post, removed the unnecessary columns, then added an image to the left out cell in the rdlc design. In the image property set the source to "External"

set the source to "External"

Now open the rdlc in xml view, there find the dataset tag, add a new dataset for the new table with images

<DataSet Name="DataSet1">
  <Fields>
    <Field Name="filepath">
      <DataField>track_file_id_pk</DataField>
      <rd:TypeName>System.string</rd:TypeName>
    </Field>
    </Field>
  </Fields>
  <Query>
    <DataSourceName>xxxt</DataSourceName>
    <CommandText>/* Local Query */</CommandText>
  </Query>
  <rd:DataSetInfo>
    <rd:DataSetName>xxx</rd:DataSetName>
    ...
  </rd:DataSetInfo>
</DataSet>

Now adding the images list dataset to the new table as shown below

<Tablix Name="Tablix2">
    ....
    </TablixRowHierarchy>
    <DataSetName>ImgDataSet</DataSetName>

Go back to design view of rdlc, go to image properties, set the "use this image" field

set the "use this image" field

In code behind create Datatable with one column "filepath", add rows with images filepath then add datasource to report below the existing datasource.

DataTable dtable = new DataTable();
DataColumn dcol = new DataColumn("filepath");
dtable.Columns.Add(dcol);

DataRow drow = dtable.NewRow();
string pat = new Uri(Server.MapPath("~/Content/DSC_019.jpg")).AbsoluteUri;
drow["track_file_uuidName"] = pat;
dtable.Rows.Add(drow);
...

ReportViewer1.LocalReport.EnableExternalImages = true;

...
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("rptDataSet", objCommonreport.ReportTable));
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dtable));
Related Topic