DateTimeColumn.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.ComponentModel;
  32. using System.Drawing;
  33. using System.Globalization;
  34. using System.Windows.Forms;
  35. using XPTable.Editors;
  36. using XPTable.Events;
  37. using XPTable.Models.Design;
  38. using XPTable.Renderers;
  39. using XPTable.Sorting;
  40. namespace XPTable.Models
  41. {
  42. /// <summary>
  43. /// Represents a Column whose Cells are displayed as a DateTime
  44. /// </summary>
  45. [DesignTimeVisible(false),
  46. ToolboxItem(false)]
  47. public class DateTimeColumn : DropDownColumn
  48. {
  49. #region Class Data
  50. /// <summary>
  51. /// Default long date format
  52. /// </summary>
  53. public static readonly string LongDateFormat = DateTimeFormatInfo.CurrentInfo.LongDatePattern;
  54. /// <summary>
  55. /// Default short date format
  56. /// </summary>
  57. public static readonly string ShortDateFormat = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
  58. /// <summary>
  59. /// Default time format
  60. /// </summary>
  61. public static readonly string TimeFormat = DateTimeFormatInfo.CurrentInfo.LongTimePattern;
  62. /// <summary>
  63. /// The format of the date and time displayed in the Cells
  64. /// </summary>
  65. private DateTimePickerFormat dateFormat;
  66. /// <summary>
  67. /// The custom date/time format string
  68. /// </summary>
  69. private string customFormat;
  70. #endregion
  71. #region Constructor
  72. /// <summary>
  73. /// Creates a new DateTimeColumn with default values
  74. /// </summary>
  75. public DateTimeColumn() : base()
  76. {
  77. this.Init();
  78. }
  79. /// <summary>
  80. /// Creates a new DateTimeColumn with the specified header text
  81. /// </summary>
  82. /// <param name="text">The text displayed in the column's header</param>
  83. public DateTimeColumn(string text) : base(text)
  84. {
  85. this.Init();
  86. }
  87. /// <summary>
  88. /// Creates a new DateTimeColumn with the specified header text and width
  89. /// </summary>
  90. /// <param name="text">The text displayed in the column's header</param>
  91. /// <param name="width">The column's width</param>
  92. public DateTimeColumn(string text, int width) : base(text, width)
  93. {
  94. this.Init();
  95. }
  96. /// <summary>
  97. /// Creates a new DateTimeColumn with the specified header text, width and visibility
  98. /// </summary>
  99. /// <param name="text">The text displayed in the column's header</param>
  100. /// <param name="width">The column's width</param>
  101. /// <param name="visible">Specifies whether the column is visible</param>
  102. public DateTimeColumn(string text, int width, bool visible) : base(text, width, visible)
  103. {
  104. this.Init();
  105. }
  106. /// <summary>
  107. /// Creates a new DateTimeColumn with the specified header text and image
  108. /// </summary>
  109. /// <param name="text">The text displayed in the column's header</param>
  110. /// <param name="image">The image displayed on the column's header</param>
  111. public DateTimeColumn(string text, Image image) : base(text, image)
  112. {
  113. this.Init();
  114. }
  115. /// <summary>
  116. /// Creates a new DateTimeColumn with the specified header text, image and width
  117. /// </summary>
  118. /// <param name="text">The text displayed in the column's header</param>
  119. /// <param name="image">The image displayed on the column's header</param>
  120. /// <param name="width">The column's width</param>
  121. public DateTimeColumn(string text, Image image, int width) : base(text, image, width)
  122. {
  123. this.Init();
  124. }
  125. /// <summary>
  126. /// Creates a new DateTimeColumn with the specified header text, image, width and visibility
  127. /// </summary>
  128. /// <param name="text">The text displayed in the column's header</param>
  129. /// <param name="image">The image displayed on the column's header</param>
  130. /// <param name="width">The column's width</param>
  131. /// <param name="visible">Specifies whether the column is visible</param>
  132. public DateTimeColumn(string text, Image image, int width, bool visible) : base(text, image, width, visible)
  133. {
  134. this.Init();
  135. }
  136. /// <summary>
  137. /// Initializes the DateTimeColumn with default values
  138. /// </summary>
  139. internal void Init()
  140. {
  141. this.dateFormat = DateTimePickerFormat.Long;
  142. this.customFormat = DateTimeFormatInfo.CurrentInfo.ShortDatePattern + " " + DateTimeFormatInfo.CurrentInfo.LongTimePattern;
  143. }
  144. #endregion
  145. #region Methods
  146. /// <summary>
  147. /// Gets a string that specifies the name of the Column's default CellRenderer
  148. /// </summary>
  149. /// <returns>A string that specifies the name of the Column's default
  150. /// CellRenderer</returns>
  151. public override string GetDefaultRendererName()
  152. {
  153. return "DATETIME";
  154. }
  155. /// <summary>
  156. /// Gets the Column's default CellRenderer
  157. /// </summary>
  158. /// <returns>The Column's default CellRenderer</returns>
  159. public override ICellRenderer CreateDefaultRenderer()
  160. {
  161. return new DateTimeCellRenderer();
  162. }
  163. /// <summary>
  164. /// Gets a string that specifies the name of the Column's default CellEditor
  165. /// </summary>
  166. /// <returns>A string that specifies the name of the Column's default
  167. /// CellEditor</returns>
  168. public override string GetDefaultEditorName()
  169. {
  170. return "DATETIME";
  171. }
  172. /// <summary>
  173. /// Gets the Column's default CellEditor
  174. /// </summary>
  175. /// <returns>The Column's default CellEditor</returns>
  176. public override ICellEditor CreateDefaultEditor()
  177. {
  178. return new DateTimeCellEditor();
  179. }
  180. #endregion
  181. #region Properties
  182. /// <summary>
  183. /// Gets or sets the format of the date and time displayed in the Column's Cells
  184. /// </summary>
  185. [Category("Appearance"),
  186. DefaultValue(DateTimePickerFormat.Long),
  187. Description("The format of the date and time displayed in the Column's Cells")]
  188. public DateTimePickerFormat DateTimeFormat
  189. {
  190. get
  191. {
  192. return this.dateFormat;
  193. }
  194. set
  195. {
  196. if (!Enum.IsDefined(typeof(DateTimePickerFormat), value))
  197. {
  198. throw new InvalidEnumArgumentException("value", (int) value, typeof(DateTimePickerFormat));
  199. }
  200. if (this.dateFormat != value)
  201. {
  202. this.dateFormat = value;
  203. this.OnPropertyChanged(new ColumnEventArgs(this, ColumnEventType.RendererChanged, null));
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// Gets or sets the custom date/time format string
  209. /// </summary>
  210. [Category("Appearance"),
  211. Description("The custom date/time format string")]
  212. public string CustomDateTimeFormat
  213. {
  214. get
  215. {
  216. return this.customFormat;
  217. }
  218. set
  219. {
  220. if (value == null)
  221. {
  222. throw new ArgumentNullException("CustomFormat cannot be null");
  223. }
  224. if (!this.customFormat.Equals(value))
  225. {
  226. this.customFormat = value;
  227. this.OnPropertyChanged(new ColumnEventArgs(this, ColumnEventType.RendererChanged, null));
  228. }
  229. DateTime.Now.ToString(DateTimeFormatInfo.CurrentInfo.ShortDatePattern);
  230. }
  231. }
  232. /// <summary>
  233. /// Specifies whether the CustomDateTimeFormat property should be serialized at
  234. /// design time
  235. /// </summary>
  236. /// <returns>true if the CustomDateTimeFormat property should be serialized,
  237. /// false otherwise</returns>
  238. private bool ShouldSerializeCustomDateTimeFormat()
  239. {
  240. return !this.customFormat.Equals(DateTimeFormatInfo.CurrentInfo.ShortDatePattern + " " + DateTimeFormatInfo.CurrentInfo.LongTimePattern);
  241. }
  242. /// <summary>
  243. /// Gets or sets the string that specifies how the Column's Cell contents
  244. /// are formatted
  245. /// </summary>
  246. [Browsable(false),
  247. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  248. public new string Format
  249. {
  250. get
  251. {
  252. return this.CustomDateTimeFormat;
  253. }
  254. set
  255. {
  256. this.CustomDateTimeFormat = value;
  257. }
  258. }
  259. /// <summary>
  260. /// Gets the Type of the Comparer used to compare the Column's Cells when
  261. /// the Column is sorting
  262. /// </summary>
  263. public override Type DefaultComparerType
  264. {
  265. get
  266. {
  267. return typeof(DateTimeComparer);
  268. }
  269. }
  270. #endregion
  271. }
  272. }