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