TreeGridNodeCollection.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //---------------------------------------------------------------------
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. //THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
  6. //KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. //IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  8. //PARTICULAR PURPOSE.
  9. //---------------------------------------------------------------------
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Windows.Forms;
  15. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  16. {
  17. public class TreeGridNodeCollection : IList<TreeGridNode>, IList
  18. {
  19. internal List<TreeGridNode> _list;
  20. internal TreeGridNode _owner;
  21. internal TreeGridNodeCollection(TreeGridNode owner)
  22. {
  23. this._owner = owner;
  24. this._list = new List<TreeGridNode>();
  25. }
  26. #region Public Members
  27. public void Add(TreeGridNode item)
  28. {
  29. // The row needs to exist in the child collection before the parent is notified.
  30. item._grid = this._owner._grid;
  31. bool hadChildren = this._owner.HasChildren;
  32. item._owner = this;
  33. this._list.Add(item);
  34. this._owner.AddChildNode(item);
  35. // if the owner didn't have children but now does (asserted) and it is sited update it
  36. if (!hadChildren && this._owner.IsSited)
  37. {
  38. this._owner._grid.InvalidateRow(this._owner.RowIndex);
  39. }
  40. }
  41. public TreeGridNode Add(string text)
  42. {
  43. TreeGridNode node = new TreeGridNode();
  44. this.Add(node);
  45. node.Cells[0].Value = text;
  46. return node;
  47. }
  48. public TreeGridNode Add(params object[] values)
  49. {
  50. TreeGridNode node = new TreeGridNode();
  51. this.Add(node);
  52. int cell = 0;
  53. if (values.Length > node.Cells.Count)
  54. throw new ArgumentOutOfRangeException("values");
  55. foreach (object o in values)
  56. {
  57. node.Cells[cell].Value = o;
  58. cell++;
  59. }
  60. return node;
  61. }
  62. // 重载方法
  63. public TreeGridNode Add(DataRow row, DataColumnCollection DataSourceColumns, DataGridViewColumnCollection columns,
  64. SortedList<int, string> sortInlist, out SortedList<int, string> sortlist, TreeGridNode pNode = null)
  65. {
  66. try
  67. {
  68. TreeGridNode node = new TreeGridNode();
  69. this.Add(node);
  70. for (int i = 0; i < columns.Count; i++)
  71. {
  72. if (!sortInlist.ContainsValue(columns[i].DataPropertyName))
  73. {
  74. if (DataSourceColumns.Contains(columns[i].DataPropertyName))
  75. {
  76. node.Cells[columns[i].DataPropertyName].Value = row[columns[i].DataPropertyName];
  77. }
  78. sortInlist.Add(node.Cells[columns[i].DataPropertyName].ColumnIndex, columns[i].DataPropertyName);
  79. }
  80. else
  81. {
  82. int e = sortInlist.IndexOfValue(columns[i].DataPropertyName);
  83. if (DataSourceColumns.Contains(columns[i].DataPropertyName))
  84. {
  85. node.Cells[e].Value = row[columns[i].DataPropertyName];
  86. }
  87. //node.Cells[e].Value = row[columns[i].DataPropertyName];
  88. }
  89. }
  90. sortlist = sortInlist;
  91. return node;
  92. }
  93. catch (Exception ex)
  94. {
  95. throw ex;
  96. }
  97. }
  98. public void Insert(int index, TreeGridNode item)
  99. {
  100. // The row needs to exist in the child collection before the parent is notified.
  101. item._grid = this._owner._grid;
  102. item._owner = this;
  103. this._list.Insert(index, item);
  104. this._owner.InsertChildNode(index, item);
  105. }
  106. public bool Remove(TreeGridNode item)
  107. {
  108. // The parent is notified first then the row is removed from the child collection.
  109. this._owner.RemoveChildNode(item);
  110. item._grid = null;
  111. return this._list.Remove(item);
  112. }
  113. public void RemoveAt(int index)
  114. {
  115. TreeGridNode row = this._list[index];
  116. // The parent is notified first then the row is removed from the child collection.
  117. this._owner.RemoveChildNode(row);
  118. row._grid = null;
  119. this._list.RemoveAt(index);
  120. }
  121. public void Clear()
  122. {
  123. // The parent is notified first then the row is removed from the child collection.
  124. this._owner.ClearNodes();
  125. this._list.Clear();
  126. }
  127. public int IndexOf(TreeGridNode item)
  128. {
  129. return this._list.IndexOf(item);
  130. }
  131. public TreeGridNode this[int index]
  132. {
  133. get
  134. {
  135. return this._list[index];
  136. }
  137. set
  138. {
  139. throw new Exception("The method or operation is not implemented.");
  140. }
  141. }
  142. public bool Contains(TreeGridNode item)
  143. {
  144. return this._list.Contains(item);
  145. }
  146. public void CopyTo(TreeGridNode[] array, int arrayIndex)
  147. {
  148. throw new Exception("The method or operation is not implemented.");
  149. }
  150. public int Count
  151. {
  152. get { return this._list.Count; }
  153. }
  154. public bool IsReadOnly
  155. {
  156. get { return false; }
  157. }
  158. #endregion
  159. #region IList Interface
  160. void System.Collections.IList.Remove(object value)
  161. {
  162. this.Remove(value as TreeGridNode);
  163. }
  164. int System.Collections.IList.Add(object value)
  165. {
  166. TreeGridNode item = value as TreeGridNode;
  167. this.Add(item);
  168. return item.Index;
  169. }
  170. void System.Collections.IList.RemoveAt(int index)
  171. {
  172. this.RemoveAt(index);
  173. }
  174. void System.Collections.IList.Clear()
  175. {
  176. this.Clear();
  177. }
  178. bool System.Collections.IList.IsReadOnly
  179. {
  180. get { return this.IsReadOnly; }
  181. }
  182. bool System.Collections.IList.IsFixedSize
  183. {
  184. get { return false; }
  185. }
  186. int System.Collections.IList.IndexOf(object item)
  187. {
  188. return this.IndexOf(item as TreeGridNode);
  189. }
  190. void System.Collections.IList.Insert(int index, object value)
  191. {
  192. this.Insert(index, value as TreeGridNode);
  193. }
  194. int System.Collections.ICollection.Count
  195. {
  196. get { return this.Count; }
  197. }
  198. bool System.Collections.IList.Contains(object value)
  199. {
  200. return this.Contains(value as TreeGridNode);
  201. }
  202. void System.Collections.ICollection.CopyTo(Array array, int index)
  203. {
  204. throw new Exception("The method or operation is not implemented.");
  205. }
  206. object System.Collections.IList.this[int index]
  207. {
  208. get
  209. {
  210. return this[index];
  211. }
  212. set
  213. {
  214. throw new Exception("The method or operation is not implemented.");
  215. }
  216. }
  217. #region IEnumerable<ExpandableRow> Members
  218. public IEnumerator<TreeGridNode> GetEnumerator()
  219. {
  220. return this._list.GetEnumerator();
  221. }
  222. #endregion
  223. #region IEnumerable Members
  224. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  225. {
  226. return this.GetEnumerator();
  227. }
  228. #endregion
  229. #endregion
  230. #region ICollection Members
  231. bool System.Collections.ICollection.IsSynchronized
  232. {
  233. get { throw new Exception("The method or operation is not implemented."); }
  234. }
  235. object System.Collections.ICollection.SyncRoot
  236. {
  237. get { throw new Exception("The method or operation is not implemented."); }
  238. }
  239. #endregion
  240. }
  241. }