1 //------------------------------------------------------------------------------
2 // <copyright file="DataViewManager.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="false" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
10 namespace System.Data {
12 using System.ComponentModel;
13 using System.Collections;
19 Designer("Microsoft.VSDesigner.Data.VS.DataViewManagerDesigner, " + AssemblyRef.MicrosoftVSDesigner)
21 public class DataViewManager : MarshalByValueComponent, IBindingList, System.ComponentModel.ITypedList {
22 private DataViewSettingCollection dataViewSettingsCollection;
23 private DataSet dataSet;
24 private DataViewManagerListItemTypeDescriptor item;
26 internal int nViews = 0;
28 private System.ComponentModel.ListChangedEventHandler onListChanged;
30 private static NotSupportedException NotSupported = new NotSupportedException();
32 public DataViewManager() : this(null, false) {}
34 public DataViewManager(DataSet dataSet) : this(dataSet, false) {}
36 internal DataViewManager(DataSet dataSet, bool locked) {
37 GC.SuppressFinalize(this);
38 this.dataSet = dataSet;
39 if (this.dataSet != null) {
40 this.dataSet.Tables.CollectionChanged += new CollectionChangeEventHandler(TableCollectionChanged);
41 this.dataSet.Relations.CollectionChanged += new CollectionChangeEventHandler(RelationCollectionChanged);
44 this.item = new DataViewManagerListItemTypeDescriptor(this);
45 this.dataViewSettingsCollection = new DataViewSettingCollection(this);
50 ResDescriptionAttribute(Res.DataViewManagerDataSetDescr)
52 public DataSet DataSet {
58 throw ExceptionBuilder.SetFailed("DataSet to null");
61 throw ExceptionBuilder.SetDataSetFailed();
63 if (dataSet != null) {
65 throw ExceptionBuilder.CanNotSetDataSet();
67 this.dataSet.Tables.CollectionChanged -= new CollectionChangeEventHandler(TableCollectionChanged);
68 this.dataSet.Relations.CollectionChanged -= new CollectionChangeEventHandler(RelationCollectionChanged);
72 this.dataSet.Tables.CollectionChanged += new CollectionChangeEventHandler(TableCollectionChanged);
73 this.dataSet.Relations.CollectionChanged += new CollectionChangeEventHandler(RelationCollectionChanged);
74 this.dataViewSettingsCollection = new DataViewSettingCollection(this);
80 DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
81 ResDescriptionAttribute(Res.DataViewManagerTableSettingsDescr)
83 public DataViewSettingCollection DataViewSettings {
85 return dataViewSettingsCollection;
89 public string DataViewSettingCollectionString {
94 StringBuilder builder = new StringBuilder();
95 builder.Append("<DataViewSettingCollectionString>");
96 foreach (DataTable dt in dataSet.Tables) {
97 DataViewSetting ds = dataViewSettingsCollection[dt];
98 builder.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "<{0} Sort=\"{1}\" RowFilter=\"{2}\" RowStateFilter=\"{3}\"/>", dt.EncodedTableName, ds.Sort, ds.RowFilter, ds.RowStateFilter);
100 builder.Append("</DataViewSettingCollectionString>");
101 return builder.ToString();
104 if (value == null || value.Length == 0)
107 XmlTextReader r = new XmlTextReader(new StringReader(value));
108 r.WhitespaceHandling = WhitespaceHandling.None;
110 if (r.Name != "DataViewSettingCollectionString")
111 throw ExceptionBuilder.SetFailed("DataViewSettingCollectionString");
113 if (r.NodeType != XmlNodeType.Element)
116 string table = XmlConvert.DecodeName(r.LocalName);
117 if (r.MoveToAttribute("Sort"))
118 dataViewSettingsCollection[table].Sort = r.Value;
120 if (r.MoveToAttribute("RowFilter"))
121 dataViewSettingsCollection[table].RowFilter = r.Value;
123 if (r.MoveToAttribute("RowStateFilter"))
124 dataViewSettingsCollection[table].RowStateFilter = (DataViewRowState)Enum.Parse(typeof(DataViewRowState),r.Value);
129 IEnumerator IEnumerable.GetEnumerator() {
130 DataViewManagerListItemTypeDescriptor[] items = new DataViewManagerListItemTypeDescriptor[1];
131 ((ICollection)this).CopyTo(items, 0);
132 return items.GetEnumerator();
135 int ICollection.Count {
141 object ICollection.SyncRoot {
147 bool ICollection.IsSynchronized {
153 bool IList.IsReadOnly {
159 bool IList.IsFixedSize {
165 void ICollection.CopyTo(Array array, int index) {
166 array.SetValue((object)(new DataViewManagerListItemTypeDescriptor(this)), index);
169 object IList.this[int index] {
174 throw ExceptionBuilder.CannotModifyCollection();
178 int IList.Add(object value) {
179 throw ExceptionBuilder.CannotModifyCollection();
183 throw ExceptionBuilder.CannotModifyCollection();
186 bool IList.Contains(object value) {
187 return(value == item);
190 int IList.IndexOf(object value) {
191 return(value == item) ? 1 : -1;
194 void IList.Insert(int index, object value) {
195 throw ExceptionBuilder.CannotModifyCollection();
198 void IList.Remove(object value) {
199 throw ExceptionBuilder.CannotModifyCollection();
202 void IList.RemoveAt(int index) {
203 throw ExceptionBuilder.CannotModifyCollection();
206 // ------------- IBindingList: ---------------------------
208 bool IBindingList.AllowNew {
213 object IBindingList.AddNew() {
217 bool IBindingList.AllowEdit {
223 bool IBindingList.AllowRemove {
229 bool IBindingList.SupportsChangeNotification {
235 bool IBindingList.SupportsSearching {
241 bool IBindingList.SupportsSorting {
247 bool IBindingList.IsSorted {
253 PropertyDescriptor IBindingList.SortProperty {
259 ListSortDirection IBindingList.SortDirection {
265 public event System.ComponentModel.ListChangedEventHandler ListChanged {
267 onListChanged += value;
270 onListChanged -= value;
274 void IBindingList.AddIndex(PropertyDescriptor property) {
278 void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) {
282 int IBindingList.Find(PropertyDescriptor property, object key) {
286 void IBindingList.RemoveIndex(PropertyDescriptor property) {
290 void IBindingList.RemoveSort() {
295 string IBindingList.GetListName() {
296 return ((System.Data.ITypedList)this).GetListName(null);
298 string IBindingList.GetListName(PropertyDescriptor[] listAccessors) {
299 return ((System.Data.ITypedList)this).GetListName(listAccessors);
303 // Microsoft: GetListName and GetItemProperties almost the same in DataView and DataViewManager
304 string System.ComponentModel.ITypedList.GetListName(PropertyDescriptor[] listAccessors) {
305 DataSet dataSet = DataSet;
307 throw ExceptionBuilder.CanNotUseDataViewManager();
309 if (listAccessors == null || listAccessors.Length == 0) {
310 return dataSet.DataSetName;
313 DataTable table = dataSet.FindTable(null, listAccessors, 0);
315 return table.TableName;
321 PropertyDescriptorCollection System.ComponentModel.ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors) {
322 DataSet dataSet = DataSet;
324 throw ExceptionBuilder.CanNotUseDataViewManager();
326 if (listAccessors == null || listAccessors.Length == 0) {
327 return((ICustomTypeDescriptor)(new DataViewManagerListItemTypeDescriptor(this))).GetProperties();
330 DataTable table = dataSet.FindTable(null, listAccessors, 0);
332 return table.GetPropertyDescriptorCollection(null);
335 return new PropertyDescriptorCollection(null);
338 public DataView CreateDataView(DataTable table) {
340 throw ExceptionBuilder.CanNotUseDataViewManager();
342 DataView dataView = new DataView(table);
343 dataView.SetDataViewManager(this);
347 protected virtual void OnListChanged(ListChangedEventArgs e) {
349 if (onListChanged != null) {
350 onListChanged(this, e);
353 catch (Exception f) {
355 if (!Common.ADP.IsCatchableExceptionType(f)) {
358 ExceptionBuilder.TraceExceptionWithoutRethrow(f);
359 // ignore the exception
363 protected virtual void TableCollectionChanged(object sender, CollectionChangeEventArgs e) {
364 PropertyDescriptor NullProp = null;
366 e.Action == CollectionChangeAction.Add ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorAdded, new DataTablePropertyDescriptor((System.Data.DataTable)e.Element)) :
367 e.Action == CollectionChangeAction.Refresh ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, NullProp) :
368 e.Action == CollectionChangeAction.Remove ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorDeleted, new DataTablePropertyDescriptor((System.Data.DataTable)e.Element)) :
373 protected virtual void RelationCollectionChanged(object sender, CollectionChangeEventArgs e) {
374 DataRelationPropertyDescriptor NullProp = null;
376 e.Action == CollectionChangeAction.Add ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorAdded, new DataRelationPropertyDescriptor((System.Data.DataRelation)e.Element)) :
377 e.Action == CollectionChangeAction.Refresh ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorChanged, NullProp):
378 e.Action == CollectionChangeAction.Remove ? new ListChangedEventArgs(ListChangedType.PropertyDescriptorDeleted, new DataRelationPropertyDescriptor((System.Data.DataRelation)e.Element)) :