2 // System.Data.DataRowCollection.cs
5 // Daniel Morgan <danmorg@sc.rr.com>
6 // Tim Coleman <tim@timcoleman.com>
8 // (C) Ximian, Inc 2002
9 // (C) Copyright 2002 Tim Coleman
10 // (C) Copyright 2002 Daniel Morgan
14 using System.Collections;
15 using System.ComponentModel;
20 /// Collection of DataRows in a DataTable
23 public class DataRowCollection : InternalDataCollectionBase
25 private DataTable table;
28 /// Internal constructor used to build a DataRowCollection.
30 internal DataRowCollection (DataTable table) : base ()
36 /// Gets the row at the specified index.
38 public DataRow this[int index]
42 throw new IndexOutOfRangeException ("There is no row at position " + index + ".");
44 return (DataRow) list[index];
49 /// This member overrides InternalDataCollectionBase.List
51 protected override ArrayList List
57 /// Adds the specified DataRow to the DataRowCollection object.
59 public void Add (DataRow row)
63 throw new ArgumentNullException("row", "'row' argument cannot be null.");
65 if (row.Table != this.table)
66 throw new ArgumentException ("This row already belongs to another table.");
68 // If row id is not -1, we know that it is in the collection.
70 throw new ArgumentException ("This row already belongs to this table.");
73 if ((table.DataSet == null || table.DataSet.EnforceConstraints) && !table._duringDataLoad)
74 // we have to check that the new row doesn't colide with existing row
75 ValidateDataRowInternal(row);
77 row.HasParentCollection = true;
80 row.RowID = list.Count - 1;
82 row.Table.ChangedDataRow (row, DataRowAction.Add);
86 /// Creates a row using specified values and adds it to the DataRowCollection.
88 public virtual DataRow Add (object[] values)
90 DataRow row = table.NewRow ();
91 row.ItemArray = values;
97 /// Clears the collection of all rows.
101 if (this.table.DataSet != null)
103 foreach (DataTable table in this.table.DataSet.Tables)
105 foreach (Constraint c in table.Constraints)
107 if (c is ForeignKeyConstraint)
109 if (((ForeignKeyConstraint) c).RelatedTable.Equals(this.table))
110 throw new InvalidConstraintException("Cannot clear table Parent because ForeignKeyConstraint " + c.ConstraintName + " enforces Child.");
119 /// Gets a value indicating whether the primary key of any row in the collection contains
120 /// the specified value.
122 public bool Contains (object key)
124 return Find (key) != null;
128 /// Gets a value indicating whether the primary key column(s) of any row in the
129 /// collection contains the values specified in the object array.
131 public bool Contains (object[] keys)
133 if (table.PrimaryKey.Length != keys.Length)
134 throw new ArgumentException ("Expecting " + table.PrimaryKey.Length + " value(s) for the key " +
135 "being indexed, but received " + keys.Length + " value(s).");
137 return Find (keys) != null;
141 /// Gets the row specified by the primary key value.
144 public DataRow Find (object key)
146 if (table.PrimaryKey.Length == 0)
147 throw new MissingPrimaryKeyException ("Table doesn't have a primary key.");
148 if (table.PrimaryKey.Length > 1)
149 throw new ArgumentException ("Expecting " + table.PrimaryKey.Length +
150 " value(s) for the key being indexed, but received 1 value(s).");
152 string primColumnName = table.PrimaryKey [0].ColumnName;
154 object newKey = null;
156 foreach (DataRow row in this) {
158 if (row.RowState != DataRowState.Deleted)
160 object primValue = row [primColumnName];
163 if (primValue == null)
169 newKey = Convert.ChangeType (key, Type.GetTypeCode(primValue.GetType ()));
171 if (primValue.Equals (newKey))
176 // FIXME: is the correct value null?
181 /// Gets the row containing the specified primary key values.
184 public DataRow Find (object[] keys)
186 if (table.PrimaryKey.Length == 0)
187 throw new MissingPrimaryKeyException ("Table doesn't have a primary key.");
189 string [] primColumnNames = new string [table.PrimaryKey.Length];
191 for (int i = 0; i < primColumnNames.Length; i++)
192 primColumnNames [i] = table.PrimaryKey [i].ColumnName;
195 object newKey = null;
197 foreach (DataRow row in this) {
199 if (row.RowState != DataRowState.Deleted)
202 for (int i = 0; i < keys.Length; i++)
205 object primValue = row [primColumnNames [i]];
206 object keyValue = keys [i];
207 if (keyValue == null)
209 if (primValue == null)
215 newKey = Convert.ChangeType (keyValue, Type.GetTypeCode(primValue.GetType ()));
217 if (!primValue.Equals (newKey))
229 // FIXME: is the correct value null?
234 /// Inserts a new row into the collection at the specified location.
236 public void InsertAt (DataRow row, int pos)
239 throw new IndexOutOfRangeException ("The row insert position " + pos + " is invalid.");
241 if (pos >= list.Count)
244 list.Insert (pos, row);
248 /// Removes the specified DataRow from the internal list. Used by DataRow to commit the removing.
250 internal void RemoveInternal (DataRow row) {
252 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
254 int index = list.IndexOf(row);
256 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
258 list.RemoveAt(index);
262 /// Removes the specified DataRow from the collection.
264 public void Remove (DataRow row)
267 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
268 int index = list.IndexOf(row);
270 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
272 // if the row was in added state it will be in Detached state after the
273 // delete operation, so we have to check it.
274 if (row.RowState != DataRowState.Detached)
279 /// Removes the row at the specified index from the collection.
281 public void RemoveAt (int index)
283 if (index < 0 || index >= list.Count)
284 throw new IndexOutOfRangeException ("There is no row at position " + index + ".");
285 DataRow row = (DataRow)list [index];
291 ///Internal method used to validate a given DataRow with respect
292 ///to the DataRowCollection
295 internal void ValidateDataRowInternal(DataRow row)
297 //first check for null violations.
298 row.CheckNullConstraints();
299 //FIXME: this validates constraints in the order they appear
300 //in the collection. Most probably we need to do it in a
301 //specific order like unique/primary keys first, then Foreignkeys, etc
302 foreach(Constraint constraint in table.Constraints)
304 constraint.AssertConstraint(row);