using System;
using System.ComponentModel;
using System.Drawing;
using FastReport.Utils;
using FastReport.Format;
using System.Drawing.Design;
namespace FastReport.Data
{
///
/// Specifies the format for the column value.
///
public enum ColumnFormat
{
///
/// The format will be determined automatically depending on the column's DataType.
///
Auto,
///
/// Specifies the General format (no formatting).
///
General,
///
/// Specifies the Number format.
///
Number,
///
/// Specifies the Currency format.
///
Currency,
///
/// Specifies the Date format.
///
Date,
///
/// Specifies the Time format.
///
Time,
///
/// Specifies the Percent format.
///
Percent,
///
/// Specifies the Boolean format.
///
Boolean
}
///
/// Specifies the type of an object that will be created when you drop the
/// data column on a report page.
///
public enum ColumnBindableControl
{
///
/// The column will create the object.
///
Text,
///
/// The column will create the object.
///
RichText,
///
/// The column will create the object.
///
Picture,
///
/// The column will create the object.
///
CheckBox,
///
/// The column will create the custom object, specified in the
/// property.
///
Custom
}
///
/// This class represents a single data column in a .
///
public partial class Column : DataComponentBase, IParent
{
#region Fields
private string propName;
private PropertyDescriptor propDescriptor;
private Type dataType;
private ColumnBindableControl bindableControl;
private ColumnFormat format;
private string customBindableControl;
private bool calculated;
private string expression;
private ColumnCollection columns;
private object tag;
#endregion
#region Properties
///
/// Gets or sets the business object property name which this column is bound to.
///
[Browsable(false)]
public string PropName
{
get { return propName; }
set { propName = value; }
}
///
/// Gets or sets the business object property descriptor which this column is bound to.
///
[Browsable(false)]
public PropertyDescriptor PropDescriptor
{
get { return propDescriptor; }
set { propDescriptor = value; }
}
///
/// Gets or sets the type of data supplied by this column.
///
[TypeConverter(typeof(FastReport.TypeConverters.DataTypeConverter))]
[Category("Data")]
[Editor("FastReport.TypeEditors.DataTypeEditor, FastReport", typeof(UITypeEditor))]
public Type DataType
{
get { return dataType; }
set { dataType = value; }
}
///
/// Gets or sets a value that specifies the type of a control that will be created
/// when you drop this column on a report page.
///
///
/// If you need to specify the custom type, use the property instead.
///
[DefaultValue(ColumnBindableControl.Text)]
[Category("Design")]
public ColumnBindableControl BindableControl
{
get { return bindableControl; }
set { bindableControl = value; }
}
///
/// Gets or sets a name of custom bindable control.
///
///
/// Use this property if you want to bind a column to custom object type. You need to
/// specify the type name of your object; that object must be registered in FastReport using the
/// RegisteredObjects.Add method.
///
[Category("Design")]
public string CustomBindableControl
{
get { return customBindableControl; }
set
{
customBindableControl = value;
if (!String.IsNullOrEmpty(value))
BindableControl = ColumnBindableControl.Custom;
}
}
///
/// Gets or sets the format of this column.
///
///
/// This property is used when you drag a column from the Data window to the report page.
/// FastReport will create a "Text" object and set its "Format" property to the corresponding format.
/// By default, this property is set to Auto. It means that the format will be determined
/// automatically depending on the property.
///
[DefaultValue(ColumnFormat.Auto)]
[Category("Design")]
public ColumnFormat Format
{
get { return format; }
set { format = value; }
}
///
/// Gets or sets expression of the calculated column.
///
///
/// This property is used if the property is true.
///
[Category("Data")]
[Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
public string Expression
{
get { return expression; }
set { expression = value; }
}
///
/// Gets or sets a value that indicates whether this column is calculated.
///
///
/// You should specify the property for calculated columns.
///
[DefaultValue(false)]
[Category("Data")]
public bool Calculated
{
get { return calculated; }
set { calculated = value; }
}
///
/// Gets the collection of child columns.
///
[Browsable(false)]
public ColumnCollection Columns
{
get { return columns; }
}
///
/// Gets or sets the tag value.
///
[Browsable(false)]
internal object Tag
{
get { return tag; }
set { tag = value; }
}
internal object Value
{
get
{
if (Calculated)
{
if (!String.IsNullOrEmpty(Expression))
return Report.Calc(Expression);
}
else
{
DataSourceBase dataSource = ParentDataSource;
if (dataSource != null)
return dataSource[this];
}
return null;
}
}
internal DataSourceBase ParentDataSource
{
get
{
Base parent = Parent;
while (parent != null)
{
if (parent is DataSourceBase)
return (parent as DataSourceBase);
parent = parent.Parent;
}
return null;
}
}
internal string FullName
{
get
{
if (Parent is Column)
return (Parent as Column).FullName + "." + Alias;
return Alias;
}
}
#endregion
#region Public Methods
///
public override void SetName(string value)
{
base.SetName(value);
if (String.IsNullOrEmpty(PropName))
PropName = Name;
}
internal Column FindByPropName(string propName)
{
foreach (Column c in Columns)
{
if (c.PropName == propName)
return c;
}
return null;
}
internal void SetBindableControlType(Type type)
{
if (type == typeof(byte[]) || typeof(Image).IsAssignableFrom(type))
BindableControl = ColumnBindableControl.Picture;
else if (type == typeof(bool))
BindableControl = ColumnBindableControl.CheckBox;
else
BindableControl = ColumnBindableControl.Text;
}
internal FormatBase GetFormat()
{
switch (Format)
{
case ColumnFormat.Auto:
if (DataType == typeof(decimal))
return new CurrencyFormat();
else if (DataType == typeof(float) || DataType == typeof(double))
return new NumberFormat();
else if (DataType == typeof(DateTime))
return new DateFormat();
break;
case ColumnFormat.Number:
return new NumberFormat();
case ColumnFormat.Currency:
return new CurrencyFormat();
case ColumnFormat.Date:
return new DateFormat();
case ColumnFormat.Time:
return new TimeFormat();
case ColumnFormat.Percent:
return new PercentFormat();
case ColumnFormat.Boolean:
return new BooleanFormat();
}
return new GeneralFormat();
}
///
public override void Serialize(FRWriter writer)
{
base.Serialize(writer);
writer.WriteValue("DataType", DataType);
if (PropName != Name)
writer.WriteStr("PropName", PropName);
if (BindableControl != ColumnBindableControl.Text)
writer.WriteValue("BindableControl", BindableControl);
if (!String.IsNullOrEmpty(CustomBindableControl))
writer.WriteStr("CustomBindableControl", CustomBindableControl);
if (Format != ColumnFormat.Auto)
writer.WriteValue("Format", Format);
if (Calculated)
{
writer.WriteBool("Calculated", Calculated);
writer.WriteStr("Expression", Expression);
}
}
///
public override string[] GetExpressions()
{
if (Calculated)
return new string[] { Expression };
return null;
}
#endregion
#region IParent Members
///
public virtual bool CanContain(Base child)
{
return child is Column;
}
///
public virtual void GetChildObjects(ObjectCollection list)
{
foreach (Column c in Columns)
{
list.Add(c);
}
}
///
public virtual void AddChild(Base child)
{
if (child is Column)
Columns.Add(child as Column);
}
///
public virtual void RemoveChild(Base child)
{
if (child is Column)
Columns.Remove(child as Column);
}
///
public int GetChildOrder(Base child)
{
return 0;
}
///
public void SetChildOrder(Base child, int order)
{
// do nothing
}
///
public void UpdateLayout(float dx, float dy)
{
// do nothing
}
#endregion
///
/// Initializes a new instance of the Column class with default settings.
///
public Column()
{
DataType = typeof(int);
PropName = "";
BindableControl = ColumnBindableControl.Text;
CustomBindableControl = "";
Expression = "";
columns = new ColumnCollection(this);
}
}
}