Insert unknown number of Images to the report at runtime using reportviewer
I'm using reportviewer and I would like to add an unknown number of images to the report on runtime. The user should select开发者_运维百科 some images (in another place) and those images should be displayed in the report one after the other. Do you have any idea how to do that using reportviewer?
thanks, Ofir
There are many ways to do this. Here is one possibility:
You will need to set up the datasource for the report dynamically, this blog post will help with that.
Your dataset needs to have a table with one column, of type string.
In the report designer (double click the rdlc file), add a table. Delete the columns until one is left. In the remaining cell, add an image. Set the image type to external. Set the image's src to an expression that is your dataset's field.
At runtime, collect all the images the user wants. These images need to be accessible to the reportviewer somewhere, on a local file system for the WinForms viewer, or on a server for the WebForms viewer. Then setup a string array containing the paths to these images as the report's datasource. The report will user these strings to find the images, and add one image for every string you give it.
I did the following to show a list of images with unknown number of items in a matrix.
First create a dataset with only one field in it, you can whatever you want , i named it "filepath" and name of dataset to be "DataSet5". You can change the name and use it accordingly.
Then you need to add a table, delete unnecessary rows and columns such that you are left with only 1 column and row (1x1) matrix. Insert an image in that column, set its properties now. Image source should be EXTERNAL.
In your aspx.cs file for the report, get the paths of the images from the database, now get absolute paths for these and append "file:///" as reports require a file to be shown. I did like this:
string rel_Path = HttpContext.Current.Server.MapPath("~");
if (_articleOrders.Any())
{
foreach (ArticleOrder order in _articleOrders)
{
if (!string.IsNullOrEmpty(order.ArticleImage))
{
if (File.Exists(rel_Path + "\\" + order.ArticleImage))
{
var AIpath = "file:///" + rel_Path + "\\" + order.ArticleImage;
articleImagesList.Add(AIpath);
}
}
}
CreateDatasetForImages(articleImagesList);
}
Then I added data to dataset like below:
private void CreateDatasetForImages(List<string> articleImagesList)
{
DataTable table = new DataTable();
table.Columns.Add("filepath", typeof(string));
foreach (string articleImage in articleImagesList)
{
DataRow drow = table.NewRow();
string pat = articleImage;
drow["filepath"] = pat;
table.Rows.Add(drow);
}
FlowerBookingReportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet5", table));
}
Now again go to image properties and set "Use This Image" to [filepath] as it the name of the column in our dataset that is holding the path of the image. Hope it works for someone!
精彩评论