Column.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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
  121. {
  122. if (value != null)
  123. dataType = value;
  124. }
  125. }
  126. /// <summary>
  127. /// Gets or sets a value that specifies the type of a control that will be created
  128. /// when you drop this column on a report page.
  129. /// </summary>
  130. /// <remarks>
  131. /// If you need to specify the custom type, use the <see cref="CustomBindableControl"/> property instead.
  132. /// </remarks>
  133. [DefaultValue(ColumnBindableControl.Text)]
  134. [Category("Design")]
  135. public ColumnBindableControl BindableControl
  136. {
  137. get { return bindableControl; }
  138. set { bindableControl = value; }
  139. }
  140. /// <summary>
  141. /// Gets or sets a name of custom bindable control.
  142. /// </summary>
  143. /// <remarks>
  144. /// Use this property if you want to bind a column to custom object type. You need to
  145. /// specify the type name of your object; that object must be registered in FastReport using the
  146. /// <b>RegisteredObjects.Add</b> method.
  147. /// </remarks>
  148. [Category("Design")]
  149. public string CustomBindableControl
  150. {
  151. get { return customBindableControl; }
  152. set
  153. {
  154. customBindableControl = value;
  155. if (!String.IsNullOrEmpty(value))
  156. BindableControl = ColumnBindableControl.Custom;
  157. }
  158. }
  159. /// <summary>
  160. /// Gets or sets the format of this column.
  161. /// </summary>
  162. /// <remarks>
  163. /// This property is used when you drag a column from the Data window to the report page.
  164. /// FastReport will create a "Text" object and set its "Format" property to the corresponding format.
  165. /// By default, this property is set to <b>Auto</b>. It means that the format will be determined
  166. /// automatically depending on the <see cref="DataType"/> property.
  167. /// </remarks>
  168. [DefaultValue(ColumnFormat.Auto)]
  169. [Category("Design")]
  170. public ColumnFormat Format
  171. {
  172. get { return format; }
  173. set { format = value; }
  174. }
  175. /// <summary>
  176. /// Gets or sets expression of the calculated column.
  177. /// </summary>
  178. /// <remarks>
  179. /// This property is used if the <see cref="Calculated"/> property is <b>true</b>.
  180. /// </remarks>
  181. [Category("Data")]
  182. [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
  183. public string Expression
  184. {
  185. get { return expression; }
  186. set { expression = value; }
  187. }
  188. /// <summary>
  189. /// Gets or sets a value that indicates whether this column is calculated.
  190. /// </summary>
  191. /// <remarks>
  192. /// You should specify the <see cref="Expression"/> property for calculated columns.
  193. /// </remarks>
  194. [DefaultValue(false)]
  195. [Category("Data")]
  196. public bool Calculated
  197. {
  198. get { return calculated; }
  199. set { calculated = value; }
  200. }
  201. /// <summary>
  202. /// Gets the collection of child columns.
  203. /// </summary>
  204. [Browsable(false)]
  205. public ColumnCollection Columns
  206. {
  207. get { return columns; }
  208. }
  209. /// <summary>
  210. /// Gets or sets the tag value.
  211. /// </summary>
  212. [Browsable(false)]
  213. internal object Tag
  214. {
  215. get { return tag; }
  216. set { tag = value; }
  217. }
  218. internal object Value
  219. {
  220. get
  221. {
  222. if (Calculated)
  223. {
  224. if (!String.IsNullOrEmpty(Expression))
  225. return Report.Calc(Expression);
  226. }
  227. else
  228. {
  229. DataSourceBase dataSource = ParentDataSource;
  230. if (dataSource != null)
  231. return dataSource[this];
  232. }
  233. return null;
  234. }
  235. }
  236. internal DataSourceBase ParentDataSource
  237. {
  238. get
  239. {
  240. Base parent = Parent;
  241. while (parent != null)
  242. {
  243. if (parent is DataSourceBase)
  244. return (parent as DataSourceBase);
  245. parent = parent.Parent;
  246. }
  247. return null;
  248. }
  249. }
  250. internal string FullName
  251. {
  252. get
  253. {
  254. if (Parent is Column)
  255. return (Parent as Column).FullName + "." + Alias;
  256. return Alias;
  257. }
  258. }
  259. #endregion
  260. #region Public Methods
  261. /// <inheritdoc/>
  262. public override void SetName(string value)
  263. {
  264. base.SetName(value);
  265. if (String.IsNullOrEmpty(PropName))
  266. PropName = Name;
  267. }
  268. internal Column FindByPropName(string propName)
  269. {
  270. foreach (Column c in Columns)
  271. {
  272. if (c.PropName == propName)
  273. return c;
  274. }
  275. return null;
  276. }
  277. internal void SetBindableControlType(Type type)
  278. {
  279. if (type == typeof(byte[]) || typeof(Image).IsAssignableFrom(type))
  280. BindableControl = ColumnBindableControl.Picture;
  281. else if (type == typeof(bool))
  282. BindableControl = ColumnBindableControl.CheckBox;
  283. else
  284. BindableControl = ColumnBindableControl.Text;
  285. }
  286. internal FormatBase GetFormat()
  287. {
  288. switch (Format)
  289. {
  290. case ColumnFormat.Auto:
  291. if (DataType == typeof(decimal))
  292. return new CurrencyFormat();
  293. else if (DataType == typeof(float) || DataType == typeof(double))
  294. return new NumberFormat();
  295. else if (DataType == typeof(DateTime))
  296. return new DateFormat();
  297. break;
  298. case ColumnFormat.Number:
  299. return new NumberFormat();
  300. case ColumnFormat.Currency:
  301. return new CurrencyFormat();
  302. case ColumnFormat.Date:
  303. return new DateFormat();
  304. case ColumnFormat.Time:
  305. return new TimeFormat();
  306. case ColumnFormat.Percent:
  307. return new PercentFormat();
  308. case ColumnFormat.Boolean:
  309. return new BooleanFormat();
  310. }
  311. return new GeneralFormat();
  312. }
  313. /// <inheritdoc/>
  314. public override void Serialize(FRWriter writer)
  315. {
  316. base.Serialize(writer);
  317. writer.WriteValue("DataType", DataType);
  318. if (PropName != Name)
  319. writer.WriteStr("PropName", PropName);
  320. if (BindableControl != ColumnBindableControl.Text)
  321. writer.WriteValue("BindableControl", BindableControl);
  322. if (!String.IsNullOrEmpty(CustomBindableControl))
  323. writer.WriteStr("CustomBindableControl", CustomBindableControl);
  324. if (Format != ColumnFormat.Auto)
  325. writer.WriteValue("Format", Format);
  326. if (Calculated)
  327. {
  328. writer.WriteBool("Calculated", Calculated);
  329. writer.WriteStr("Expression", Expression);
  330. }
  331. }
  332. /// <inheritdoc/>
  333. public override string[] GetExpressions()
  334. {
  335. if (Calculated)
  336. return new string[] { Expression };
  337. return null;
  338. }
  339. #endregion
  340. #region IParent Members
  341. /// <inheritdoc/>
  342. public virtual bool CanContain(Base child)
  343. {
  344. return child is Column;
  345. }
  346. /// <inheritdoc/>
  347. public virtual void GetChildObjects(ObjectCollection list)
  348. {
  349. foreach (Column c in Columns)
  350. {
  351. list.Add(c);
  352. }
  353. }
  354. /// <inheritdoc/>
  355. public virtual void AddChild(Base child)
  356. {
  357. if (child is Column)
  358. Columns.Add(child as Column);
  359. }
  360. /// <inheritdoc/>
  361. public virtual void RemoveChild(Base child)
  362. {
  363. if (child is Column)
  364. Columns.Remove(child as Column);
  365. }
  366. /// <inheritdoc/>
  367. public int GetChildOrder(Base child)
  368. {
  369. return 0;
  370. }
  371. /// <inheritdoc/>
  372. public void SetChildOrder(Base child, int order)
  373. {
  374. // do nothing
  375. }
  376. /// <inheritdoc/>
  377. public void UpdateLayout(float dx, float dy)
  378. {
  379. // do nothing
  380. }
  381. #endregion
  382. /// <summary>
  383. /// Initializes a new instance of the <b>Column</b> class with default settings.
  384. /// </summary>
  385. public Column()
  386. {
  387. DataType = typeof(int);
  388. PropName = "";
  389. BindableControl = ColumnBindableControl.Text;
  390. CustomBindableControl = "";
  391. Expression = "";
  392. columns = new ColumnCollection(this);
  393. }
  394. }
  395. }