// // System.Data.DataRowCollection.cs // // Author: // Daniel Morgan // Tim Coleman // // (C) Ximian, Inc 2002 // (C) Copyright 2002 Tim Coleman // (C) Copyright 2002 Daniel Morgan // using System; using System.Collections; using System.ComponentModel; namespace System.Data { /// /// Collection of DataRows in a DataTable /// [Serializable] public class DataRowCollection : InternalDataCollectionBase { private DataTable table; /// /// Internal constructor used to build a DataRowCollection. /// internal DataRowCollection (DataTable table) : base () { this.table = table; } /// /// Gets the row at the specified index. /// public DataRow this[int index] { get { if (index >= Count) throw new IndexOutOfRangeException ("There is no row at position " + index + "."); return (DataRow) list[index]; } } /// /// This member overrides InternalDataCollectionBase.List /// protected override ArrayList List { get { return list; } } /// /// Adds the specified DataRow to the DataRowCollection object. /// public void Add (DataRow row) { //TODO: AutoIncrement //TODO: validation list.Add (row); row.AttachRow (); row.Table.ChangedDataRow (row, DataRowAction.Add); } /// /// Creates a row using specified values and adds it to the DataRowCollection. /// public virtual DataRow Add (object[] values) { DataRow row = table.NewRow (); row.ItemArray = values; Add (row); return row; } /// /// Clears the collection of all rows. /// public void Clear () { list.Clear (); } /// /// Gets a value indicating whether the primary key of any row in the collection contains /// the specified value. /// public bool Contains (object key) { return Find (key) != null; } /// /// Gets a value indicating whether the primary key column(s) of any row in the /// collection contains the values specified in the object array. /// public bool Contains (object[] keys) { return Find (keys) != null; } /// /// Gets the row specified by the primary key value. /// [MonoTODO] public DataRow Find (object key) { throw new NotImplementedException (); } /// /// Gets the row containing the specified primary key values. /// [MonoTODO] public DataRow Find (object[] keys) { throw new NotImplementedException (); } /// /// Inserts a new row into the collection at the specified location. /// public void InsertAt (DataRow row, int pos) { list.Insert (pos, row); } /// /// Removes the specified DataRow from the collection. /// public void Remove (DataRow row) { list.Remove (row); row.DetachRow (); table.DeletedDataRow (row, DataRowAction.Delete); } /// /// Removes the row at the specified index from the collection. /// public void RemoveAt (int index) { DataRow row = (DataRow)list [index]; list.RemoveAt (index); table.DeletedDataRow (row, DataRowAction.Delete); } /// ///Internal method used to validate a given DataRow with respect ///to the DataRowCollection /// [MonoTODO] internal void ValidateDataRowInternal(DataRow row) { //FIXME: this validates constraints in the order they appear //in the collection. Most probably we need to do it in a //specific order like unique/primary keys first, then Foreignkeys, etc foreach(Constraint constraint in table.Constraints) { constraint.AssertConstraint(row); } } } }