G2DTreeDataGridCell.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using AdvancedDataGridView;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace CommonFroms.Utils.TreeGrid
  9. {
  10. public class G2DTreeGridValueColumn : DataGridViewTextBoxColumn
  11. {
  12. public G2DTreeGridValueColumn()
  13. {
  14. this.CellTemplate = new G2DTreeGridValueCell();
  15. }
  16. public override DataGridViewCell CellTemplate
  17. {
  18. get
  19. {
  20. return base.CellTemplate;
  21. }
  22. set
  23. {
  24. G2DTreeGridValueCell cell = value as G2DTreeGridValueCell;
  25. if (value != null && cell == null)
  26. {
  27. throw new InvalidCastException("Value provided for CellTemplate must be of type TEditNumDataGridViewCell or derive from it.");
  28. }
  29. base.CellTemplate = value;
  30. }
  31. }
  32. }
  33. public class G2DTreeGridValueCell : DataGridViewTextBoxCell
  34. {
  35. public G2DTreeGridValueCell()
  36. {
  37. }
  38. private Type editType = typeof(TextG2DTreeGridValueEditingControl);
  39. private Type valueType = typeof(string);
  40. public void SetValueEditType(Type vtype, Type etype = null)
  41. {
  42. this.valueType = vtype;
  43. if (etype != null)
  44. {
  45. this.editType = etype;
  46. }
  47. else
  48. {
  49. if (valueType.IsEnum)
  50. {
  51. this.editType = typeof(EnumG2DTreeGridValueEditingControl);
  52. }
  53. else if (typeof(bool).IsAssignableFrom(ValueType))
  54. {
  55. this.editType = typeof(BoolG2DTreeGridValueEditingControl);
  56. }
  57. else
  58. {
  59. this.editType = typeof(TextG2DTreeGridValueEditingControl);
  60. }
  61. }
  62. }
  63. public override Type EditType
  64. {
  65. get { return editType; }
  66. }
  67. public override Type ValueType
  68. {
  69. get { return valueType; }
  70. }
  71. public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.ComponentModel.TypeConverter valueTypeConverter)
  72. {
  73. return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
  74. }
  75. }
  76. public class TextG2DTreeGridValueEditingControl : DataGridViewTextBoxEditingControl
  77. {
  78. public TextG2DTreeGridValueEditingControl()
  79. {
  80. }
  81. }
  82. public class EnumG2DTreeGridValueEditingControl : DataGridViewComboBoxEditingControl
  83. {
  84. public EnumG2DTreeGridValueEditingControl()
  85. {
  86. }
  87. public void SetEnumType(Type type)
  88. {
  89. if (type.IsEnum)
  90. {
  91. base.Items.Clear();
  92. foreach (object obj in Enum.GetValues(type))
  93. {
  94. base.Items.Add(obj.ToString());
  95. }
  96. }
  97. }
  98. public G2DTreeGridValueCell CurrentCell
  99. {
  100. get
  101. {
  102. return EditingControlDataGridView[2, EditingControlRowIndex] as G2DTreeGridValueCell;
  103. }
  104. }
  105. public override void PrepareEditingControlForEdit(bool selectAll)
  106. {
  107. this.Text = CurrentCell.Value + "";
  108. SetEnumType(CurrentCell.ValueType);
  109. }
  110. }
  111. public class BoolG2DTreeGridValueEditingControl : DataGridViewComboBoxEditingControl
  112. {
  113. public BoolG2DTreeGridValueEditingControl()
  114. {
  115. base.Items.Add(true.ToString());
  116. base.Items.Add(false.ToString());
  117. }
  118. public G2DTreeGridValueCell CurrentCell
  119. {
  120. get
  121. {
  122. return EditingControlDataGridView[2, EditingControlRowIndex] as G2DTreeGridValueCell;
  123. }
  124. }
  125. public override void PrepareEditingControlForEdit(bool selectAll)
  126. {
  127. this.Text = CurrentCell.Value + "";
  128. }
  129. }
  130. public class MoneyTextBoxDataGridViewEditingControl : TextBox, IDataGridViewEditingControl
  131. {
  132. private DataGridView dataGridView; // grid owning this editing control
  133. private bool valueChanged; // editing control's value has changed or not
  134. private int rowIndex; // row index in which the editing control resides
  135. #region IDataGridViewEditingControl 成员
  136. public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
  137. {
  138. this.Font = dataGridViewCellStyle.Font;
  139. if (dataGridViewCellStyle.BackColor.A < 255)
  140. {
  141. // The NumericUpDown control does not support transparent back colors
  142. Color opaqueBackColor = Color.FromArgb(255, dataGridViewCellStyle.BackColor);
  143. this.BackColor = opaqueBackColor;
  144. this.dataGridView.EditingPanel.BackColor = opaqueBackColor;
  145. }
  146. else
  147. {
  148. this.BackColor = dataGridViewCellStyle.BackColor;
  149. }
  150. this.ForeColor = dataGridViewCellStyle.ForeColor;
  151. }
  152. public DataGridView EditingControlDataGridView
  153. {
  154. get
  155. {
  156. return this.dataGridView;
  157. }
  158. set
  159. {
  160. this.dataGridView = value;
  161. }
  162. }
  163. public object EditingControlFormattedValue
  164. {
  165. get
  166. {
  167. return GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting);
  168. }
  169. set
  170. {
  171. this.Text = (string)value;
  172. }
  173. }
  174. public int EditingControlRowIndex
  175. {
  176. get
  177. {
  178. return this.rowIndex;
  179. }
  180. set
  181. {
  182. this.rowIndex = value;
  183. }
  184. }
  185. public bool EditingControlValueChanged
  186. {
  187. get { return this.valueChanged; }
  188. set { this.valueChanged = value; }
  189. }
  190. public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
  191. {
  192. switch (keyData & Keys.KeyCode)
  193. {
  194. case Keys.Right:
  195. case Keys.Left:
  196. case Keys.Down:
  197. case Keys.Up:
  198. case Keys.Home:
  199. case Keys.End:
  200. case Keys.Delete:
  201. return true;
  202. }
  203. return !dataGridViewWantsInputKey;
  204. }
  205. public Cursor EditingPanelCursor
  206. {
  207. get { return Cursors.Default; }
  208. }
  209. public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
  210. {
  211. return this.Text;
  212. }
  213. public void PrepareEditingControlForEdit(bool selectAll)
  214. {
  215. if (selectAll)
  216. {
  217. this.SelectAll();
  218. }
  219. else
  220. {
  221. this.SelectionStart = this.Text.Length;
  222. }
  223. }
  224. public bool RepositionEditingControlOnValueChange
  225. {
  226. get { return false; }
  227. }
  228. #endregion
  229. }
  230. }