Skip to content

Latest commit

 

History

History
132 lines (107 loc) · 7.56 KB

creating-a-custom-report-item-run-time-component.md

File metadata and controls

132 lines (107 loc) · 7.56 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic ms.custom helpviewer_keywords
Creating a custom report item run-time component
Learn how to create a custom report item run-time component and define the properties for that component in the design environment.
kfollis
kfollis
09/25/2024
reporting-services
custom-report-items
reference
updatefrequency5
custom report items, creating

Creating a custom report item run-time component

The custom report item run-time component is implemented as a [!INCLUDEmsCoName] [!INCLUDEdnprdnshort] component using any CLS-compliant language, and is called by the report processor at run time. You define the properties for the run-time component in the design environment by modifying the custom report item's corresponding design-time component.

For a sample of a fully implemented custom report item, see SQL Server Reporting Services Product Samples.

Definition and instance objects

Before implementing a custom report item, it's important to understand the difference between definition objects and instance objects. Definition objects provide the RDL representation of the custom report item whereas instance objects are the evaluated versions of the definition objects. There's only one definition object for each item on the report. When accessing properties on a definition object that contain expressions, you get the unevaluated expression string. Instance objects contain the evaluated versions of the definition objects and can have a one-to-many relationship with an item's definition object. For example, if a report has a xref:Microsoft.ReportingServices.OnDemandReportRendering.Tablix data region that contains a xref:Microsoft.ReportingServices.OnDemandReportRendering.CustomReportItem in a detail row, there will be only one definition object but there will be an instance object for each row in the data region.

Implement the ICustomReportItem interface

To create a CustomReportItem run-time component, you need to implement the xref:Microsoft.ReportingServices.OnDemandReportRendering.ICustomReportItem interface that is defined in the Microsoft.ReportingServices.ProcessingCore.dll:

namespace Microsoft.ReportingServices.OnDemandReportRendering  
{  
    public interface ICustomReportItem  
    {  
        void GenerateReportItemDefinition(CustomReportItem customReportItem);  
void EvaluateReportItemInstance(CustomReportItem customReportItem);  
    }  
}  

After you implement the xref:Microsoft.ReportingServices.OnDemandReportRendering.ICustomReportItem interface, two method stubs will be generated for you: xref:Microsoft.ReportingServices.OnDemandReportRendering.ICustomReportItem.GenerateReportItemDefinition%2A and xref:Microsoft.ReportingServices.OnDemandReportRendering.ICustomReportItem.EvaluateReportItemInstance%2A. The xref:Microsoft.ReportingServices.OnDemandReportRendering.ICustomReportItem.GenerateReportItemDefinition%2A method is called first and is used for setting definition properties and creating the xref:Microsoft.ReportingServices.OnDemandReportRendering.Image object that contains both the definition and instance properties that are used for rendering the item. The xref:Microsoft.ReportingServices.OnDemandReportRendering.ICustomReportItem.EvaluateReportItemInstance%2A method is called after the definition objects are evaluated, and it provides the instance objects that will be used for rendering the item.

The following example implementation shows a custom report item that renders the name of the control as an image.

namespace Microsoft.Samples.ReportingServices  
{  
    using System;  
    using System.Collections.Generic;  
    using System.Collections.Specialized;  
    using System.Drawing.Imaging;  
    using System.IO;  
    using System.Text;  
    using Microsoft.ReportingServices.OnDemandReportRendering;  
  
    public class PolygonsCustomReportItem : ICustomReportItem  
    {  
        #region ICustomReportItem Members  
  
        public void GenerateReportItemDefinition(CustomReportItem cri)  
        {  
            // Create the Image object that will be   
            // used to render the custom report item  
            cri.CreateCriImageDefinition();  
            Image polygonImage = (Image)cri.GeneratedReportItem;  
        }  
  
        public void EvaluateReportItemInstance(CustomReportItem cri)  
        {  
            // Get the Image definition  
            Image polygonImage = (Image)cri.GeneratedReportItem;  
  
            // Create the image for the custom report item  
            polygonImage.ImageInstance.ImageData = DrawImage(cri);  
        }  
  
        #endregion  
  
        /// <summary>  
        /// Creates an image of the CustomReportItem's name  
        /// </summary>  
        private byte[] DrawImage(CustomReportItem customReportItem)  
        {  
            int width = 1;          // pixels  
            int height = 1;         // pixels  
            int resolution = 75;    // dpi  
  
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);  
            bitmap.SetResolution(resolution, resolution);  
  
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);  
            graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;  
  
            // Get the Font for the Text  
            System.Drawing.Font font = new System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace,  
                12, System.Drawing.FontStyle.Regular);  
  
            // Get the Brush for drawing the Text  
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen);  
  
            // Get the measurements for the image  
            System.Drawing.SizeF maxStringSize = graphics.MeasureString(customReportItem.Name, font);  
            width = (int)(maxStringSize.Width + 2 * font.GetHeight(resolution));  
            height = (int)(maxStringSize.Height + 2 * font.GetHeight(resolution));  
  
            bitmap.Dispose();  
            bitmap = new System.Drawing.Bitmap(width, height);  
            bitmap.SetResolution(resolution, resolution);  
  
            graphics.Dispose();  
            graphics = System.Drawing.Graphics.FromImage(bitmap);  
            graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;  
  
            // Draw the text  
            graphics.DrawString(customReportItem.Name, font, brush, font.GetHeight(resolution),   
                font.GetHeight(resolution));  
  
            // Create the byte array of the image data  
            MemoryStream memoryStream = new MemoryStream();  
            bitmap.Save(memoryStream, ImageFormat.Bmp);  
            memoryStream.Position = 0;  
            byte[] imageData = new byte[memoryStream.Length];  
            memoryStream.Read(imageData, 0, imageData.Length);  
  
            return imageData;  
        }  
    }  
}  

Related content