Column.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. using FastReport.Format;
  6. using System.Drawing.Design;
  7. namespace FastReport.Data
  8. {
  9. /// <summary>
  10. /// Specifies the format for the column value.
  11. /// </summary>
  12. public enum ColumnFormat
  13. {
  14. /// <summary>
  15. /// The format will be determined automatically depending on the column's DataType.
  16. /// </summary>
  17. Auto,
  18. /// <summary>
  19. /// Specifies the General format (no formatting).
  20. /// </summary>
  21. General,
  22. /// <summary>
  23. /// Specifies the Number format.
  24. /// </summary>
  25. Number,
  26. /// <summary>
  27. /// Specifies the Currency format.
  28. /// </summary>
  29. Currency,
  30. /// <summary>
  31. /// Specifies the Date format.
  32. /// </summary>
  33. Date,
  34. /// <summary>
  35. /// Specifies the Time format.
  36. /// </summary>
  37. Time,
  38. /// <summary>
  39. /// Specifies the Percent format.
  40. /// </summary>
  41. Percent,
  42. /// <summary>
  43. /// Specifies the Boolean format.
  44. /// </summary>
  45. Boolean
  46. }
  47. /// <summary>
  48. /// Specifies the type of an object that will be created when you drop the
  49. /// data column on a report page.
  50. /// </summary>
  51. public enum ColumnBindableControl
  52. {
  53. /// <summary>
  54. /// The column will create the <see cref="TextObject"/> object.
  55. /// </summary>
  56. Text,
  57. /// <summary>
  58. /// The column will create the <see cref="RichObject"/> object.
  59. /// </summary>
  60. RichText,
  61. /// <summary>
  62. /// The column will create the <see cref="PictureObject"/> object.
  63. /// </summary>
  64. Picture,
  65. /// <summary>
  66. /// The column will create the <see cref="CheckBoxObject"/> object.
  67. /// </summary>
  68. CheckBox,
  69. /// <summary>
  70. /// The column will create the custom object, specified in the
  71. /// <see cref="Column.CustomBindableControl"/> property.
  72. /// </summary>
  73. Custom
  74. }
  75. /// <summary>
  76. /// This class represents a single data column in a <see cref="DataSourceBase"/>.
  77. /// </summary>
  78. public partial class Column : DataComponentBase, IParent
  79. {
  80. #region Fields
  81. private string propName;
  82. private PropertyDescriptor propDescriptor;
  83. private Type dataType;
  84. private ColumnBindableControl bindableControl;
  85. private ColumnFormat format;
  86. private string customBindableControl;
  87. private bool calculated;
  88. private string expression;
  89. private ColumnCollection columns;
  90. private object tag;
  91. #endregion
  92. #region Properties
  93. /// <summary>
  94. /// Gets or sets the business object property name which this column is bound to.
  95. /// </summary>
  96. [Browsable(false)]
  97. public string PropName
  98. {
  99. get { return propName; }
  100. set { propName = value; }
  101. }
  102. /// <summary>
  103. /// Gets or sets the business object property descriptor which this column is bound to.
  104. /// </summary>
  105. [Browsable(false)]
  106. public PropertyDescriptor PropDescriptor
  107. {
  108. get { return propDescriptor; }
  109. set { propDescriptor = value; }
  110. }
  111. /// <summary>
  112. /// Gets or sets the type of data supplied by this column.
  113. /// </summary>
  114. [TypeConverter(typeof(FastReport.TypeConverters.DataTypeConverter))]
  115. [Category("Data")]
  116. [Editor("FastReport.TypeEditors.DataTypeEditor, FastReport", typeof(UITypeEditor))]
  117. public Type DataType
  118. {
  119. get { return dataType; }
  120. set { dataType = value; }
  121. }
  122. /// <summary>
  123. /// Gets or sets a value that specifies the type of a control that will be created
  124. /// when you drop this column on a report page.
  125. /// </summary>
  126. /// <remarks>
  127. /// If you need to specify the custom type, use the <see cref="CustomBindableControl"/> property instead.
  128. /// </remarks>
  129. [DefaultValue(ColumnBindableControl.Text)]
  130. [Category("Design")]
  131. public ColumnBindableControl BindableControl
  132. {
  133. get { return bindableControl; }
  134. set { bindableControl = value; }
  135. }
  136. /// <summary>
  137. /// Gets or sets a name of custom bindable control.
  138. /// </summary>
  139. /// <remarks>
  140. /// Use this property if you want to bind a column to custom object type. You need to
  141. /// specify the type name of your object; that object must be registered in FastReport using the
  142. /// <b>RegisteredObjects.Add</b> method.
  143. /// </remarks>
  144. [Category("Design")]
  145. public string CustomBindableControl
  146. {
  147. get { return customBindableControl; }
  148. set
  149. {
  150. customBindableControl = value;
  151. if (!String.IsNullOrEmpty(value))
  152. BindableControl = ColumnBindableControl.Custom;
  153. }
  154. }
  155. /// <summary>
  156. /// Gets or sets the format of this column.
  157. /// </summary>
  158. /// <remarks>
  159. /// This property is used when you drag a column from the Data window to the report page.
  160. /// FastReport will create a "Text" object and set its "Format" property to the corresponding format.
  161. /// By default, this property is set to <b>Auto</b>. It means that the format will be determined
  162. /// automatically depending on the <see cref="DataType"/> property.
  163. /// </remarks>
  164. [DefaultValue(ColumnFormat.Auto)]
  165. [Category("Design")]
  166. public ColumnFormat Format
  167. {
  168. get { return format; }
  169. set { format = value; }
  170. }
  171. /// <summary>
  172. /// Gets or sets expression of the calculated column.
  173. /// </summary>
  174. /// <remarks>
  175. /// This property is used if the <see cref="Calculated"/> property is <b>true</b>.
  176. /// </remarks>
  177. [Category("Data")]
  178. [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
  179. public string Expression
  180. {
  181. get { return expression; }
  182. set { expression = value; }
  183. }
  184. /// <summary>
  185. /// Gets or sets a value that indicates whether this column is calculated.
  186. /// </summary>
  187. /// <remarks>
  188. /// You should specify the <see cref="Expression"/> property for calculated columns.
  189. /// </remarks>
  190. [DefaultValue(false)]
  191. [Category("Data")]
  192. public bool Calculated
  193. {
  194. get { return calculated; }
  195. set { calculated = value; }
  196. }
  197. /// <summary>
  198. /// Gets the collection of child columns.
  199. /// </summary>
  200. [Browsable(false)]
  201. public ColumnCollection Columns
  202. {
  203. get { return columns; }
  204. }
  205. /// <summary>
  206. /// Gets or sets the tag value.
  207. /// </summary>
  208. [Browsable(false)]
  209. internal object Tag
  210. {
  211. get { return tag; }
  212. set { tag = value; }
  213. }
  214. internal object Value
  215. {
  216. get
  217. {
  218. if (Calculated)
  219. {
  220. if (!String.IsNullOrEmpty(Expression))
  221. return Report.Calc(Expression);
  222. }
  223. else
  224. {
  225. DataSourceBase dataSource = ParentDataSource;
  226. if (dataSource != null)
  227. return dataSource[this];
  228. }
  229. return null;
  230. }
  231. }
  232. internal DataSourceBase ParentDataSource
  233. {
  234. get
  235. {
  236. Base parent = Parent;
  237. while (parent != null)
  238. {
  239. if (parent is DataSourceBase)
  240. return (parent as DataSourceBase);
  241. parent = parent.Parent;
  242. }
  243. return null;
  244. }
  245. }
  246. internal string FullName
  247. {
  248. get
  249. {
  250. if (Parent is Column)
  251. return (Parent as Column).FullName + "." + Alias;
  252. return Alias;
  253. }
  254. }
  255. #endregion
  256. #region Public Methods
  257. /// <inheritdoc/>
  258. public override void SetName(string value)
  259. {
  260. base.SetName(value);
  261. if (String.IsNullOrEmpty(PropName))
  262. PropName = Name;
  263. }
  264. internal Column FindByPropName(string propName)
  265. {
  266. foreach (Column c in Columns)
  267. {
  268. if (c.PropName == propName)
  269. return c;
  270. }
  271. return null;
  272. }
  273. internal void SetBindableControlType(Type type)
  274. {
  275. if (type == typeof(byte[]) || typeof(Image).IsAssignableFrom(type))
  276. BindableControl = ColumnBindableControl.Picture;
  277. else if (type == typeof(bool))
  278. BindableControl = ColumnBindableControl.CheckBox;
  279. else
  280. BindableControl = ColumnBindableControl.Text;
  281. }
  282. internal FormatBase GetFormat()
  283. {
  284. switch (Format)
  285. {
  286. case ColumnFormat.Auto:
  287. if (DataType == typeof(decimal))
  288. return new CurrencyFormat();
  289. else if (DataType == typeof(float) || DataType == typeof(double))
  290. return new NumberFormat();
  291. else if (DataType == typeof(DateTime))
  292. return new DateFormat();
  293. break;
  294. case ColumnFormat.Number:
  295. return new NumberFormat();
  296. case ColumnFormat.Currency:
  297. return new CurrencyFormat();
  298. case ColumnFormat.Date:
  299. return new DateFormat();
  300. case ColumnFormat.Time:
  301. return new TimeFormat();
  302. case ColumnFormat.Percent:
  303. return new PercentFormat();
  304. case ColumnFormat.Boolean:
  305. return new BooleanFormat();
  306. }
  307. return new GeneralFormat();
  308. }
  309. /// <inheritdoc/>
  310. public override void Serialize(FRWriter writer)
  311. {
  312. base.Serialize(writer);
  313. writer.WriteValue("DataType", DataType);
  314. if (PropName != Name)
  315. writer.WriteStr("PropName", PropName);
  316. if (BindableControl != ColumnBindableControl.Text)
  317. writer.WriteValue("BindableControl", BindableControl);
  318. if (!String.IsNullOrEmpty(CustomBindableControl))
  319. writer.WriteStr("CustomBindableControl", CustomBindableControl);
  320. if (Format != ColumnFormat.Auto)
  321. writer.WriteValue("Format", Format);
  322. if (Calculated)
  323. {
  324. writer.WriteBool("Calculated", Calculated);
  325. writer.WriteStr("Expression", Expression);
  326. }
  327. }
  328. /// <inheritdoc/>
  329. public override string[] GetExpressions()
  330. {
  331. if (Calculated)
  332. return new string[] { Expression };
  333. return null;
  334. }
  335. #endregion
  336. #region IParent Members
  337. /// <inheritdoc/>
  338. public virtual bool CanContain(Base child)
  339. {
  340. return child is Column;
  341. }
  342. /// <inheritdoc/>
  343. public virtual void GetChildObjects(ObjectCollection list)
  344. {
  345. foreach (Column c in Columns)
  346. {
  347. list.Add(c);
  348. }
  349. }
  350. /// <inheritdoc/>
  351. public virtual void AddChild(Base child)
  352. {
  353. if (child is Column)
  354. Columns.Add(child as Column);
  355. }
  356. /// <inheritdoc/>
  357. public virtual void RemoveChild(Base child)
  358. {
  359. if (child is Column)
  360. Columns.Remove(child as Column);
  361. }
  362. /// <inheritdoc/>
  363. public int GetChildOrder(Base child)
  364. {
  365. return 0;
  366. }
  367. /// <inheritdoc/>
  368. public void SetChildOrder(Base child, int order)
  369. {
  370. // do nothing
  371. }
  372. /// <inheritdoc/>
  373. public void UpdateLayout(float dx, float dy)
  374. {
  375. // do nothing
  376. }
  377. #endregion
  378. /// <summary>
  379. /// Initializes a new instance of the <b>Column</b> class with default settings.
  380. /// </summary>
  381. public Column()
  382. {
  383. DataType = typeof(int);
  384. PropName = "";
  385. BindableControl = ColumnBindableControl.Text;
  386. CustomBindableControl = "";
  387. Expression = "";
  388. columns = new ColumnCollection(this);
  389. }
  390. }
  391. }