DataViewManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //------------------------------------------------------------------------------
  2. // <copyright file="DataViewManager.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">[....]</owner>
  6. // <owner current="true" primary="false">[....]</owner>
  7. // <owner current="false" primary="false">[....]</owner>
  8. //------------------------------------------------------------------------------
  9. namespace System.Data {
  10. using System;
  11. using System.ComponentModel;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Text;
  15. using System.Xml;
  16. [
  17. Designer("Microsoft.VSDesigner.Data.VS.DataViewManagerDesigner, " + AssemblyRef.MicrosoftVSDesigner)
  18. ]
  19. public class DataViewManager : MarshalByValueComponent, IBindingList, System.ComponentModel.ITypedList {
  20. private DataViewSettingCollection dataViewSettingsCollection;
  21. private DataSet dataSet;
  22. private DataViewManagerListItemTypeDescriptor item;
  23. private bool locked;
  24. internal int nViews = 0;
  25. private System.ComponentModel.ListChangedEventHandler onListChanged;
  26. private static NotSupportedException NotSupported = new NotSupportedException();
  27. public DataViewManager() : this(null, false) {}
  28. public DataViewManager(DataSet dataSet) : this(dataSet, false) {}
  29. internal DataViewManager(DataSet dataSet, bool locked) {
  30. GC.SuppressFinalize(this);
  31. this.dataSet = dataSet;
  32. if (this.dataSet != null) {
  33. this.dataSet.Tables.CollectionChanged += new CollectionChangeEventHandler(TableCollectionChanged);
  34. this.dataSet.Relations.CollectionChanged += new CollectionChangeEventHandler(RelationCollectionChanged);
  35. }
  36. this.locked = locked;
  37. this.item = new DataViewManagerListItemTypeDescriptor(this);
  38. this.dataViewSettingsCollection = new DataViewSettingCollection(this);
  39. }
  40. [
  41. DefaultValue(null),
  42. ResDescriptionAttribute(Res.DataViewManagerDataSetDescr)
  43. ]
  44. public DataSet DataSet {
  45. get {
  46. return dataSet;
  47. }
  48. set {
  49. if (value == null)
  50. throw ExceptionBuilder.SetFailed("DataSet to null");
  51. if (locked)
  52. throw ExceptionBuilder.SetDataSetFailed();
  53. if (dataSet != null) {
  54. if (nViews > 0)
  55. throw ExceptionBuilder.CanNotSetDataSet();
  56. this.dataSet.Tables.CollectionChanged -= new CollectionChangeEventHandler(TableCollectionChanged);
  57. this.dataSet.Relations.CollectionChanged -= new CollectionChangeEventHandler(RelationCollectionChanged);
  58. }
  59. this.dataSet = value;
  60. this.dataSet.Tables.CollectionChanged += new CollectionChangeEventHandler(TableCollectionChanged);
  61. this.dataSet.Relations.CollectionChanged += new CollectionChangeEventHandler(RelationCollectionChanged);
  62. this.dataViewSettingsCollection = new DataViewSettingCollection(this);
  63. item.Reset();
  64. }
  65. }
  66. [
  67. DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  68. ResDescriptionAttribute(Res.DataViewManagerTableSettingsDescr)
  69. ]
  70. public DataViewSettingCollection DataViewSettings {
  71. get {
  72. return dataViewSettingsCollection;
  73. }
  74. }
  75. public string DataViewSettingCollectionString {
  76. get {
  77. if (dataSet == null)
  78. return "";
  79. StringBuilder builder = new StringBuilder();
  80. builder.Append("<DataViewSettingCollectionString>");
  81. foreach (DataTable dt in dataSet.Tables) {
  82. DataViewSetting ds = dataViewSettingsCollection[dt];
  83. builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "<{0} Sort=\"{1}\" RowFilter=\"{2}\" RowStateFilter=\"{3}\"/>", dt.EncodedTableName, ds.Sort, ds.RowFilter, ds.RowStateFilter);
  84. }
  85. builder.Append("</DataViewSettingCollectionString>");
  86. return builder.ToString();
  87. }
  88. set {
  89. if (value == null || value.Length == 0)
  90. return;
  91. XmlTextReader r = new XmlTextReader(new StringReader(value));
  92. r.WhitespaceHandling = WhitespaceHandling.None;
  93. r.Read();
  94. if (r.Name != "DataViewSettingCollectionString")
  95. throw ExceptionBuilder.SetFailed("DataViewSettingCollectionString");
  96. while (r.Read()) {
  97. if (r.NodeType != XmlNodeType.Element)
  98. continue;
  99. string table = XmlConvert.DecodeName(r.LocalName);
  100. if (r.MoveToAttribute("Sort"))
  101. dataViewSettingsCollection[table].Sort = r.Value;
  102. if (r.MoveToAttribute("RowFilter"))
  103. dataViewSettingsCollection[table].RowFilter = r.Value;
  104. if (r.MoveToAttribute("RowStateFilter"))
  105. dataViewSettingsCollection[table].RowStateFilter = (DataViewRowState)Enum.Parse(typeof(DataViewRowState),r.Value);
  106. }
  107. }
  108. }
  109. IEnumerator IEnumerable.GetEnumerator() {
  110. DataViewManagerListItemTypeDescriptor[] items = new DataViewManagerListItemTypeDescriptor[1];
  111. ((ICollection)this).CopyTo(items, 0);
  112. return items.GetEnumerator();
  113. }
  114. int ICollection.Count {
  115. get {
  116. return 1;
  117. }
  118. }
  119. object ICollection.SyncRoot {
  120. get {
  121. return this;
  122. }
  123. }
  124. bool ICollection.IsSynchronized {
  125. get {
  126. return false;
  127. }
  128. }
  129. bool IList.IsReadOnly {
  130. get {
  131. return true;
  132. }
  133. }
  134. bool IList.IsFixedSize {
  135. get {
  136. return true;
  137. }
  138. }
  139. void ICollection.CopyTo(Array array, int index) {
  140. array.SetValue((object)(new DataViewManagerListItemTypeDescriptor(this)), index);
  141. }
  142. object IList.this[int index] {
  143. get {
  144. return item;
  145. }
  146. set {
  147. throw ExceptionBuilder.CannotModifyCollection();
  148. }
  149. }
  150. int IList.Add(object value) {
  151. throw ExceptionBuilder.CannotModifyCollection();
  152. }
  153. void IList.Clear() {
  154. throw ExceptionBuilder.CannotModifyCollection();
  155. }
  156. bool IList.Contains(object value) {
  157. return(value == item);
  158. }
  159. int IList.IndexOf(object value) {
  160. return(value == item) ? 1 : -1;
  161. }
  162. void IList.Insert(int index, object value) {
  163. throw ExceptionBuilder.CannotModifyCollection();
  164. }
  165. void IList.Remove(object value) {
  166. throw ExceptionBuilder.CannotModifyCollection();
  167. }
  168. void IList.RemoveAt(int index) {
  169. throw ExceptionBuilder.CannotModifyCollection();
  170. }
  171. // ------------- IBindingList: ---------------------------
  172. bool IBindingList.AllowNew {
  173. get {
  174. return false;
  175. }
  176. }
  177. object IBindingList.AddNew() {
  178. throw NotSupported;
  179. }
  180. bool IBindingList.AllowEdit {
  181. get {
  182. return false;
  183. }
  184. }
  185. bool IBindingList.AllowRemove {
  186. get {
  187. return false;
  188. }
  189. }
  190. bool IBindingList.SupportsChangeNotification {
  191. get {
  192. return true;
  193. }
  194. }
  195. bool IBindingList.SupportsSearching {
  196. get {
  197. return false;
  198. }
  199. }
  200. bool IBindingList.SupportsSorting {
  201. get {
  202. return false;
  203. }
  204. }
  205. bool IBindingList.IsSorted {
  206. get {
  207. throw NotSupported;
  208. }
  209. }
  210. PropertyDescriptor IBindingList.SortProperty {
  211. get {
  212. throw NotSupported;
  213. }
  214. }
  215. ListSortDirection IBindingList.SortDirection {
  216. get {
  217. throw NotSupported;
  218. }
  219. }
  220. public event System.ComponentModel.ListChangedEventHandler ListChanged {
  221. add {
  222. onListChanged += value;
  223. }
  224. remove {
  225. onListChanged -= value;
  226. }
  227. }
  228. void IBindingList.AddIndex(PropertyDescriptor property) {
  229. // no operation
  230. }
  231. void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) {
  232. throw NotSupported;
  233. }
  234. int IBindingList.Find(PropertyDescriptor property, object key) {
  235. throw NotSupported;
  236. }
  237. void IBindingList.RemoveIndex(PropertyDescriptor property) {
  238. // no operation
  239. }
  240. void IBindingList.RemoveSort() {
  241. throw NotSupported;
  242. }
  243. /*
  244. string IBindingList.GetListName() {
  245. return ((System.Data.ITypedList)this).GetListName(null);
  246. }
  247. string IBindingList.GetListName(PropertyDescriptor[] listAccessors) {
  248. return ((System.Data.ITypedList)this).GetListName(listAccessors);
  249. }
  250. */
  251. // [....]: GetListName and GetItemProperties almost the same in DataView and DataViewManager
  252. string System.ComponentModel.ITypedList.GetListName(PropertyDescriptor[] listAccessors) {
  253. DataSet dataSet = DataSet;
  254. if (dataSet == null)
  255. throw ExceptionBuilder.CanNotUseDataViewManager();
  256. if (listAccessors == null || listAccessors.Length == 0) {
  257. return dataSet.DataSetName;
  258. }
  259. else {
  260. DataTable table = dataSet.FindTable(null, listAccessors, 0);
  261. if (table != null) {
  262. return table.TableName;
  263. }
  264. }
  265. return String.Empty;
  266. }
  267. PropertyDescriptorCollection System.ComponentModel.ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors) {
  268. DataSet dataSet = DataSet;
  269. if (dataSet == null)
  270. throw ExceptionBuilder.CanNotUseDataViewManager();
  271. if (listAccessors == null || listAccessors.Length == 0) {
  272. return((ICustomTypeDescriptor)(new DataViewManagerListItemTypeDescriptor(this))).GetProperties();
  273. }
  274. else {
  275. DataTable table = dataSet.FindTable(null, listAccessors, 0);
  276. if (table != null) {
  277. return table.GetPropertyDescriptorCollection(null);
  278. }
  279. }
  280. return new PropertyDescriptorCollection(null);
  281. }
  282. public DataView CreateDataView(DataTable table) {
  283. if (dataSet == null)
  284. throw ExceptionBuilder.CanNotUseDataViewManager();
  285. DataView dataView = new DataView(table);
  286. dataView.SetDataViewManager(this);
  287. return dataView;
  288. }
  289. protected virtual void OnListChanged(ListChangedEventArgs e) {
  290. try {
  291. if (onListChanged != null) {
  292. onListChanged(this, e);
  293. }
  294. }
  295. catch (Exception f) {
  296. //
  297. if (!Common.ADP.IsCatchableExceptionType(f)) {
  298. throw;
  299. }
  300. ExceptionBuilder.TraceExceptionWithoutRethrow(f);
  301. // ignore the exception
  302. }
  303. }
  304. protected virtual void TableCollectionChanged(object sender, CollectionChangeEventArgs e) {
  305. PropertyDescriptor NullProp = null;
  306. OnListChanged(
  307. e.Action == CollectionChangeAction.Add ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorAdded, new DataTablePropertyDescriptor((System.Data.DataTable)e.Element)) :
  308. e.Action == CollectionChangeAction.Refresh ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, NullProp) :
  309. e.Action == CollectionChangeAction.Remove ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorDeleted, new DataTablePropertyDescriptor((System.Data.DataTable)e.Element)) :
  310. /*default*/ null
  311. );
  312. }
  313. protected virtual void RelationCollectionChanged(object sender, CollectionChangeEventArgs e) {
  314. DataRelationPropertyDescriptor NullProp = null;
  315. OnListChanged(
  316. e.Action == CollectionChangeAction.Add ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorAdded, new DataRelationPropertyDescriptor((System.Data.DataRelation)e.Element)) :
  317. e.Action == CollectionChangeAction.Refresh ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, NullProp):
  318. e.Action == CollectionChangeAction.Remove ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorDeleted, new DataRelationPropertyDescriptor((System.Data.DataRelation)e.Element)) :
  319. /*default*/ null
  320. );
  321. }
  322. }
  323. }