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
|
//
// System.Data.DataRowCollection.cs
//
// Author:
// Daniel Morgan <danmorg@sc.rr.com>
// Tim Coleman <tim@timcoleman.com>
//
// (C) Ximian, Inc 2002
// (C) Copyright 2002 Tim Coleman
// (C) Copyright 2002 Daniel Morgan
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.ComponentModel;
using System.Data.Common;
namespace System.Data
{
/// <summary>
/// Collection of DataRows in a DataTable
/// </summary>
[Serializable]
public class DataRowCollection : InternalDataCollectionBase
{
private DataTable table;
internal event ListChangedEventHandler ListChanged;
/// <summary>
/// Internal constructor used to build a DataRowCollection.
/// </summary>
internal DataRowCollection (DataTable table) : base ()
{
this.table = table;
}
/// <summary>
/// Gets the row at the specified index.
/// </summary>
public DataRow this[int index]
{
get {
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException ("There is no row at position " + index + ".");
return (DataRow) List[index];
}
}
/// <summary>
/// This member overrides InternalDataCollectionBase.List
/// </summary>
protected override ArrayList List
{
get { return base.List; }
}
/// <summary>
/// Adds the specified DataRow to the DataRowCollection object.
/// </summary>
public void Add (DataRow row)
{
//TODO: validation
if (row == null)
throw new ArgumentNullException("row", "'row' argument cannot be null.");
if (row.Table != this.table)
throw new ArgumentException ("This row already belongs to another table.");
// If row id is not -1, we know that it is in the collection.
if (row.RowID != -1)
throw new ArgumentException ("This row already belongs to this table.");
row.BeginEdit();
row.Validate();
AddInternal(row);
}
internal void AddInternal(DataRow row) {
row.Table.ChangingDataRow (row, DataRowAction.Add);
row.HasParentCollection = true;
List.Add (row);
// Set the row id.
row.RowID = List.Count - 1;
row.AttachRow ();
row.Table.ChangedDataRow (row, DataRowAction.Add);
}
/// <summary>
/// Creates a row using specified values and adds it to the DataRowCollection.
/// </summary>
#if NET_2_0
public virtual DataRow Add (params object[] values)
#else
public virtual DataRow Add (object[] values)
#endif
{
if (values == null)
throw new NullReferenceException ();
DataRow row = table.NewNotInitializedRow();
int newRecord = table.CreateRecord(values);
row.ImportRecord(newRecord);
row.Validate();
AddInternal (row);
return row;
}
/// <summary>
/// Clears the collection of all rows.
/// </summary>
public void Clear ()
{
if (this.table.DataSet != null && this.table.DataSet.EnforceConstraints) {
foreach (DataTable table in this.table.DataSet.Tables) {
foreach (Constraint c in table.Constraints) {
if (c is ForeignKeyConstraint) {
ForeignKeyConstraint fk = (ForeignKeyConstraint) c;
if (fk.RelatedTable.Equals(this.table)
&& fk.Table.Rows.Count > 0) // check does not make sense if we don't have rows
#if NET_1_1
throw new InvalidConstraintException (String.Format ("Cannot clear table Parent" +
" because ForeignKeyConstraint "+
"{0} enforces Child.",
c.ConstraintName));
#else
throw new ArgumentException (String.Format ("Cannot clear table Parent because " +
"ForeignKeyConstraint {0} enforces Child.",
c.ConstraintName));
#endif
}
}
}
}
// Remove from indexes
for (int i = 0; i < this.Count; i++)
this.table.DeleteRowFromIndexes (this [i]);
List.Clear ();
OnListChanged (this, new ListChangedEventArgs (ListChangedType.Reset, -1, -1));
}
/// <summary>
/// Gets a value indicating whether the primary key of any row in the collection contains
/// the specified value.
/// </summary>
public bool Contains (object key)
{
return Find (key) != null;
}
/// <summary>
/// Gets a value indicating whether the primary key column(s) of any row in the
/// collection contains the values specified in the object array.
/// </summary>
public bool Contains (object[] keys)
{
return Find (keys) != null;
}
/// <summary>
/// Gets the row specified by the primary key value.
/// </summary>
public DataRow Find (object key)
{
return Find(new object[]{key});
}
/// <summary>
/// Gets the row containing the specified primary key values.
/// </summary>
public DataRow Find (object[] keys)
{
if (table.PrimaryKey.Length == 0)
throw new MissingPrimaryKeyException ("Table doesn't have a primary key.");
if (keys == null)
throw new ArgumentException ("Expecting " + table.PrimaryKey.Length +" value(s) for the key being indexed, but received 0 value(s).");
Index index = table.GetIndex(table.PrimaryKey,null,DataViewRowState.None,null,false);
int record = index.Find(keys);
return (record != -1) ? table.RecordCache[record] : null;
}
/// <summary>
/// Gets the row containing the specified primary key values by searching the rows
/// filtered by the state.
/// </summary>
internal DataRow Find (object [] keys, DataViewRowState rowStateFilter)
{
if (table.PrimaryKey.Length == 0)
throw new MissingPrimaryKeyException ("Table doesn't have a primary key.");
if (keys == null)
throw new ArgumentException ("Expecting " + table.PrimaryKey.Length +" value(s) for the key being indexed, but received 0 value(s).");
Index index = table.FindIndex (table.PrimaryKey, null, rowStateFilter, null);
if (index == null)
index = new Index (new Key (table, table.PrimaryKey, null, rowStateFilter, null));
int record = index.Find(keys);
return (record != -1) ? table.RecordCache[record] : null;
}
internal DataRow Find(int index)
{
DataColumn[] primaryKey = table.PrimaryKey;
Index primaryKeyIndex = table.FindIndex(primaryKey);
// if we can search through index
if (primaryKeyIndex != null) {
// get the child rows from the index
int record = primaryKeyIndex.Find(index);
if ( record != -1 ) {
return table.RecordCache[record];
}
}
else {
//loop through all collection rows
foreach (DataRow row in this) {
if (row.RowState != DataRowState.Deleted) {
int rowIndex = row.IndexFromVersion(DataRowVersion.Default);
bool match = true;
for (int columnCnt = 0; columnCnt < primaryKey.Length; ++columnCnt) {
if (primaryKey[columnCnt].DataContainer.CompareValues(rowIndex, index) != 0) {
match = false;
}
}
if ( match ) {
return row;
}
}
}
}
return null;
}
/// <summary>
/// Inserts a new row into the collection at the specified location.
/// </summary>
public void InsertAt (DataRow row, int pos)
{
if (pos < 0)
throw new IndexOutOfRangeException ("The row insert position " + pos + " is invalid.");
if (row == null)
throw new ArgumentNullException("row", "'row' argument cannot be null.");
if (row.Table != this.table)
throw new ArgumentException ("This row already belongs to another table.");
// If row id is not -1, we know that it is in the collection.
if (row.RowID != -1)
throw new ArgumentException ("This row already belongs to this table.");
row.Validate();
row.Table.ChangingDataRow (row, DataRowAction.Add);
if (pos >= List.Count) {
row.RowID = List.Count;
List.Add (row);
}
else {
List.Insert (pos, row);
row.RowID = pos;
for (int i = pos+1; i < List.Count; i++) {
((DataRow)List [i]).RowID = i;
}
}
row.HasParentCollection = true;
row.AttachRow ();
row.Table.ChangedDataRow (row, DataRowAction.Add);
}
/// <summary>
/// Removes the specified DataRow from the internal list. Used by DataRow to commit the removing.
/// </summary>
internal void RemoveInternal (DataRow row) {
if (row == null) {
throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
}
int index = List.IndexOf(row);
if (index < 0) {
throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
}
List.RemoveAt(index);
}
/// <summary>
/// Removes the specified DataRow from the collection.
/// </summary>
public void Remove (DataRow row)
{
if (!List.Contains(row))
throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
DataRowState state = row.RowState;
if (state != DataRowState.Deleted &&
state != DataRowState.Detached) {
row.Delete();
// if the row was in added state it will be in Detached state after the
// delete operation, so we have to check it.
if (row.RowState != DataRowState.Detached)
row.AcceptChanges();
}
}
/// <summary>
/// Removes the row at the specified index from the collection.
/// </summary>
public void RemoveAt (int index)
{
Remove(this[index]);
}
private void OnListChanged (object sender, ListChangedEventArgs args)
{
if (ListChanged != null)
ListChanged (sender, args);
}
}
}
|