Bind CustomComponent to Data

Stimulsoft Reports.NET discussion
Post Reply
Roland.K
Posts: 12
Joined: Wed Oct 01, 2008 4:39 am

Bind CustomComponent to Data

Post by Roland.K »

Hi!

We are a Software development company and currently use CrystalReports.NET as integrated reporting solution for our software. We are completely unsatisfied with this, because we want to display custom data (own datatype, binary) from our database in the reports, which seemed impossible within CR (we only managed to get a very slow hard to use solution).

After a whole lot of search I finally found Stimulsoft Reports.NET and (almost) quickly found a solution to be able to transform the data into viewable objects by using a Custom Component.

We are almost about to buy Stimulsoft Reports.NET, the only problem I'm still having is how to bind data to the CustomComponent.

Here is the changed code from your example, please provide me with information on how to be able to get data attached in the designer to this component like I do with a simple textbox.

Code: Select all

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using Stimulsoft.Base;
using Stimulsoft.Base.Drawing;
using Stimulsoft.Base.Serializing;
using Stimulsoft.Base.Services;
using Stimulsoft.Base.Design;
using Stimulsoft.Base.Localization;
using Stimulsoft.Report.Components;
using Stimulsoft.Report.Events;
using Stimulsoft.Report.Components.Design;
using Datos.WTNT.Util;

namespace CustomComponent
{

	[StiServiceBitmap(typeof(MyCustomComponent), "CustomComponent.MyCustomComponent1.gif")]
	[StiToolbox(true)]
	[StiContextTool(typeof(IStiShift))]
	[StiContextTool(typeof(IStiGrowToHeight))]
	[StiDesigner(typeof(MyCustomComponentDesigner))]
	public class MyCustomComponent : StiComponent, IStiBorder, IStiBrush
	{
        private WTComp wtc;
        #region StiComponent override
		/// 
		/// Gets value to sort a position in the toolbox.
		/// 
		public override int ToolboxPosition
		{
			get
			{
				return 500;
			}
		}

		/// 
		/// Gets a localized name of the component category.
		/// 
		public override string LocalizedCategory
		{
			get 
			{
				return StiLocalization.Get("Report", "Components");
			}
		}

		
		/// 
		/// Gets a localized component name.
		/// 
		public override string LocalizedName
		{
			get 
			{
				return "MyCustomComponent1";
			}
		}
		#endregion

		#region Render override
		/// 
		/// Rendering of the component without events.
		/// 
		/// Rendered component.
		/// Panel in which rendering will be done.
		/// Is rendering finished or not.
		protected override bool RenderComponent(ref StiComponent renderedComponent, StiContainer outContainer)
		{
			MyCustomComponent component = (MyCustomComponent)this.Clone();
			component.InvokeEvents();		
			outContainer.Components.Add(component);
			renderedComponent = component;
			return true;
		}
		#endregion

		#region IStiBorder
		private StiBorder border = new StiBorder();
		/// 
		/// Gets or sets frame of the component.
		/// 
		[StiCategory("Appearance")]
		[StiSerializable]
		[Description("Gets or sets frame of the component.")]
		public StiBorder Border
		{
			get 
			{
				return border;
			}
			set 
			{
				border = value;
			}
		}
		#endregion

		#region IStiBrush
		private StiBrush brush = new StiSolidBrush(Color.Transparent);
		/// 
		/// Gets or sets a brush to fill a component.
		/// 
		[StiCategory("Appearance")]
		[StiSerializable]
		[Description("Gets or sets a brush to fill a component.")]
		public StiBrush Brush
		{
			get 
			{
				return brush;
			}
			set 
			{
				brush = value;
			}
		}
		#endregion

		#region Paint
		/// 
		/// Paints a component.
		/// 
		/// Argument for painting.
		public override void Paint(StiPaintEventArgs e)
		{
			InvokePainting(this, e);
			
            if (!e.Cancel)
            {			
                Graphics g = e.Graphics;

                RectangleD rect = GetPaintRectangle();
                if (rect.Width > 0 && rect.Height > 0 && (e.ClipRectangle.IsEmpty || rect.IntersectsWith(e.ClipRectangle)))
                {
                    //#region Fill rectangle
                    //if (this.Brush is StiSolidBrush &&
                    //    ((StiSolidBrush)this.Brush).Color == Color.Transparent &&
                    //    Report.Info.FillComponent &&
                    //    IsDesigning)
                    //{
                    //    Color color = Color.FromArgb(150, Color.Green);

                    //    StiDrawing.FillRectangle(g, color, rect.Left, rect.Top, rect.Width, rect.Height);
                    //}
                    //else StiDrawing.FillRectangle(g, Brush, rect);
                    //#endregion

                    //******************
                    //Draw control
                    //******************
                    //wtc.Width = (int)rect.Width;
                    //wtc.Height = (int)rect.Height;

                    #region Markers
                    PaintMarkers(g, rect);
                    #endregion

                    #region Border
                    if (this.HighlightState == StiHighlightState.Hide)
                        Border.Draw(g, rect, Page.Zoom);
                    #endregion
                    System.IO.MemoryStream ms = new System.IO.MemoryStream(wtc.get_png((int)rect.Width, (int)rect.Height, 100));
                    e.Graphics.DrawImage(new Bitmap(ms), (int)rect.Location.X, (int)rect.Location.Y);

                    PaintEvents(e.Graphics, rect);
                }

            }
            //e.Cancel = false;
			InvokePainted(this, e);

		}
		#endregion

		#region ICloneable override
		/// 
		/// Creates a new object that is a copy of the current instance.
		/// 
		/// A new object that is a copy of this instance.
		public override object Clone()
		{
			return base.Clone();
		}
		#endregion

		#region this
		/// 
		/// Creates a new component of the type MyCustomComponent.
		/// 
		public MyCustomComponent() : this(RectangleD.Empty)
		{
		}

		/// 
		/// Creates a new component of the type MyCustomComponent.
		/// 
		/// The rectangle describes size and position of the component.
		public MyCustomComponent(RectangleD rect) : base(rect)
		{
        }
		#endregion
	}
}
the function needed to set the data from the database to the "WTComp" object would be

Code: Select all

			wtc.set_bin_data((byte[])DatabaseValueGoesHere);
Edward
Posts: 2913
Joined: Fri Jun 09, 2006 4:02 am

Bind CustomComponent to Data

Post by Edward »

Hello, Roland.

Yes, the component you tried to adapt to you requirements was not intended for displaying any data from DataSource.
There is another 'MyCustomComponentWithDataSource' class which has the DataSource property. Using this property you could access to data from that 'WTComp' database which also must be declared in the Dictionary.

If you need any help with creation of that component, please send a request with your sample data to support[at]stimulsoft.com and we'll help you with it.

Thank you.
Vital
Posts: 1278
Joined: Fri Jun 09, 2006 4:04 am

Bind CustomComponent to Data

Post by Vital »

You can use following code to add DataColumn property:

Code: Select all

private string dataColumn = string.Empty;
///
/// Gets or sets a name of the column that contains the image.
///
[StiSerializable]
[Editor(typeof(Stimulsoft.Report.Components.Design.StiImageDataColumnEditor), typeof(UITypeEditor))]
[StiCategory("Image")]
[DefaultValue("")]
[Description("Gets or sets a name of the column that contains the image.")]
public string DataColumn
{
  get
  {
     return dataColumn;
  }
  set
  {
    dataColumn = value;
  }
}
After then you can use following code to get data from column:

Code: Select all

object imageObject = StiDataColumn.GetDataFromDataColumn(
this.Report.Dictionary, this.DataColumn);
Let me know if you need any help.

Thank you.
Post Reply