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 (list.IndexOf(row) != -1)
69 throw new ArgumentException ("This row already belongs to this table.");
72 if ((table.DataSet == null || table.DataSet.EnforceConstraints) && !table._duringDataLoad)
73 // we have to check that the new row doesn't colide with existing row
74 ValidateDataRowInternal(row);
76 row.HasParentCollection = true;
79 row.Table.ChangedDataRow (row, DataRowAction.Add);
83 /// Creates a row using specified values and adds it to the DataRowCollection.
85 public virtual DataRow Add (object[] values)
87 DataRow row = table.NewRow ();
88 row.ItemArray = values;
94 /// Clears the collection of all rows.
98 if (this.table.DataSet != null)
100 foreach (DataTable table in this.table.DataSet.Tables)
102 foreach (Constraint c in table.Constraints)
104 if (c is ForeignKeyConstraint)
106 if (((ForeignKeyConstraint) c).RelatedTable.Equals(this.table))
107 throw new InvalidConstraintException("Cannot clear table Parent because ForeignKeyConstraint " + c.ConstraintName + " enforces Child.");
116 /// Gets a value indicating whether the primary key of any row in the collection contains
117 /// the specified value.
119 public bool Contains (object key)
121 return Find (key) != null;
125 /// Gets a value indicating whether the primary key column(s) of any row in the
126 /// collection contains the values specified in the object array.
128 public bool Contains (object[] keys)
130 if (table.PrimaryKey.Length != keys.Length)
131 throw new ArgumentException ("Expecting " + table.PrimaryKey.Length + " value(s) for the key " +
132 "being indexed, but received " + keys.Length + " value(s).");
134 return Find (keys) != null;
138 /// Gets the row specified by the primary key value.
141 public DataRow Find (object key)
143 if (table.PrimaryKey.Length == 0)
144 throw new MissingPrimaryKeyException ("Table doesn't have a primary key.");
145 if (table.PrimaryKey.Length > 1)
146 throw new ArgumentException ("Expecting " + table.PrimaryKey.Length +
147 " value(s) for the key being indexed, but received 1 value(s).");
149 string primColumnName = table.PrimaryKey [0].ColumnName;
151 object newKey = null;
153 foreach (DataRow row in this) {
155 if (row.RowState != DataRowState.Deleted)
157 object primValue = row [primColumnName];
160 if (primValue == null)
166 newKey = Convert.ChangeType (key, Type.GetTypeCode(primValue.GetType ()));
168 if (primValue.Equals (newKey))
173 // FIXME: is the correct value null?
178 /// Gets the row containing the specified primary key values.
181 public DataRow Find (object[] keys)
183 if (table.PrimaryKey.Length == 0)
184 throw new MissingPrimaryKeyException ("Table doesn't have a primary key.");
186 string [] primColumnNames = new string [table.PrimaryKey.Length];
188 for (int i = 0; i < primColumnNames.Length; i++)
189 primColumnNames [i] = table.PrimaryKey [i].ColumnName;
192 object newKey = null;
194 foreach (DataRow row in this) {
196 if (row.RowState != DataRowState.Deleted)
199 for (int i = 0; i < keys.Length; i++)
202 object primValue = row [primColumnNames [i]];
203 object keyValue = keys [i];
204 if (keyValue == null)
206 if (primValue == null)
212 newKey = Convert.ChangeType (keyValue, Type.GetTypeCode(primValue.GetType ()));
214 if (!primValue.Equals (newKey))
226 // FIXME: is the correct value null?
231 /// Inserts a new row into the collection at the specified location.
233 public void InsertAt (DataRow row, int pos)
236 throw new IndexOutOfRangeException ("The row insert position " + pos + " is invalid.");
238 if (pos >= list.Count)
241 list.Insert (pos, row);
245 /// Removes the specified DataRow from the internal list. Used by DataRow to commit the removing.
247 internal void RemoveInternal (DataRow row) {
249 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
251 int index = list.IndexOf(row);
253 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
255 list.RemoveAt(index);
259 /// Removes the specified DataRow from the collection.
261 public void Remove (DataRow row)
264 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
265 int index = list.IndexOf(row);
267 throw new IndexOutOfRangeException ("The given datarow is not in the current DataRowCollection.");
269 // if the row was in added state it will be in Detached state after the
270 // delete operation, so we have to check it.
271 if (row.RowState != DataRowState.Detached)
276 /// Removes the row at the specified index from the collection.
278 public void RemoveAt (int index)
280 if (index < 0 || index >= list.Count)
281 throw new IndexOutOfRangeException ("There is no row at position " + index + ".");
282 DataRow row = (DataRow)list [index];
288 ///Internal method used to validate a given DataRow with respect
289 ///to the DataRowCollection
292 internal void ValidateDataRowInternal(DataRow row)
294 //first check for null violations.
295 row.CheckNullConstraints();
296 //FIXME: this validates constraints in the order they appear
297 //in the collection. Most probably we need to do it in a
298 //specific order like unique/primary keys first, then Foreignkeys, etc
299 foreach(Constraint constraint in table.Constraints)
301 constraint.AssertConstraint(row);