2 // System.Data.DataRow.cs
5 // Rodrigo Moya <rodrigo@ximian.com>
6 // Daniel Morgan <danmorg@sc.rr.com>
7 // Tim Coleman <tim@timcoleman.com>
8 // Ville Palo <vi64pa@koti.soon.fi>
9 // Alan Tam Siu Lung <Tam@SiuLung.com>
11 // (C) Ximian, Inc 2002
12 // (C) Daniel Morgan 2002, 2003
13 // Copyright (C) 2002 Tim Coleman
17 using System.Collections;
18 using System.Globalization;
20 namespace System.Data {
22 /// Represents a row of data in a DataTable.
29 private DataTable _table;
31 private object[] original;
32 private object[] proposed;
33 private object[] current;
35 private string[] columnErrors;
36 private string rowError;
37 private DataRowState rowState;
38 internal int xmlRowID = 0;
39 internal bool _nullConstraintViolation;
40 private string _nullConstraintMessage;
41 private bool editing = false;
42 private bool _hasParentCollection;
43 private bool _inChangingEvent;
51 /// This member supports the .NET Framework infrastructure and is not intended to be
52 /// used directly from your code.
54 protected internal DataRow (DataRowBuilder builder)
56 _table = builder.Table;
57 // Get the row id from the builder.
58 _rowId = builder._rowId;
62 proposed = new object[_table.Columns.Count];
63 // Initialise the data coloumns of the row with the dafault values, if any
64 for (int c = 0; c < _table.Columns.Count; c++)
66 if(_table.Columns [c].DefaultValue == null)
67 proposed[c] = DBNull.Value;
69 proposed [c] = _table.Columns[c].DefaultValue;
72 columnErrors = new string[_table.Columns.Count];
73 rowError = String.Empty;
75 //on first creating a DataRow it is always detached.
76 rowState = DataRowState.Detached;
78 foreach (DataColumn Col in _table.Columns) {
80 if (Col.AutoIncrement) {
81 this [Col] = Col.AutoIncrementValue();
84 _table.Columns.CollectionChanged += new System.ComponentModel.CollectionChangeEventHandler(CollectionChanged);
93 /// Gets a value indicating whether there are errors in a row.
95 public bool HasErrors {
97 if (RowError != string.Empty)
100 for (int i= 0; i < columnErrors.Length; i++){
101 if (columnErrors[i] != null && columnErrors[i] != string.Empty)
110 /// Gets or sets the data stored in the column specified by name.
112 public object this[string columnName] {
113 get { return this[columnName, DataRowVersion.Default]; }
115 int columnIndex = _table.Columns.IndexOf (columnName);
116 if (columnIndex == -1)
117 throw new IndexOutOfRangeException ();
118 this[columnIndex] = value;
123 /// Gets or sets the data stored in specified DataColumn
125 public object this[DataColumn column] {
128 return this[column, DataRowVersion.Default];}
130 int columnIndex = _table.Columns.IndexOf (column);
131 if (columnIndex == -1)
132 throw new ArgumentException ("The column does not belong to this table.");
133 this[columnIndex] = value;
138 /// Gets or sets the data stored in column specified by index.
140 public object this[int columnIndex] {
141 get { return this[columnIndex, DataRowVersion.Default]; }
143 if (columnIndex < 0 || columnIndex > _table.Columns.Count)
144 throw new IndexOutOfRangeException ();
145 if (rowState == DataRowState.Deleted)
146 throw new DeletedRowInaccessibleException ();
147 DataColumn column = _table.Columns[columnIndex];
148 _table.ChangingDataColumn (this, column, value);
151 bool orginalEditing = editing;
152 if (!orginalEditing) BeginEdit ();
153 object v = SetColumnValue (value, columnIndex);
154 proposed[columnIndex] = v;
155 _table.ChangedDataColumn (this, column, v);
156 if (!orginalEditing) EndEdit ();
161 /// Gets the specified version of data stored in the named column.
163 public object this[string columnName, DataRowVersion version] {
165 int columnIndex = _table.Columns.IndexOf (columnName);
166 if (columnIndex == -1)
167 throw new IndexOutOfRangeException ();
168 return this[columnIndex, version];
173 /// Gets the specified version of data stored in the specified DataColumn.
175 public object this[DataColumn column, DataRowVersion version] {
177 if (column.Table != Table)
178 throw new ArgumentException ("The column does not belong to this table.");
179 int columnIndex = column.Ordinal;
180 return this[columnIndex, version];
185 /// Gets the data stored in the column, specified by index and version of the data to
188 public object this[int columnIndex, DataRowVersion version] {
190 if (columnIndex < 0 || columnIndex > _table.Columns.Count)
191 throw new IndexOutOfRangeException ();
192 // Accessing deleted rows
193 if (rowState == DataRowState.Deleted && version != DataRowVersion.Original)
194 throw new DeletedRowInaccessibleException ("Deleted row information cannot be accessed through the row.");
196 //if (rowState == DataRowState.Detached && version == DataRowVersion.Default)
197 // throw new RowNotInTableException("This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.");
198 // Non-existent version
199 //if (!HasVersion (version))
200 // throw new VersionNotFoundException (Locale.GetText ("There is no " + version.ToString () + " data to access."));
201 if (HasVersion(version))
205 case DataRowVersion.Default:
206 if (editing || rowState == DataRowState.Detached)
207 return proposed[columnIndex];
208 return current[columnIndex];
209 case DataRowVersion.Proposed:
210 return proposed[columnIndex];
211 case DataRowVersion.Current:
212 return current[columnIndex];
213 case DataRowVersion.Original:
214 return original[columnIndex];
216 throw new ArgumentException ();
219 if (rowState == DataRowState.Detached && version == DataRowVersion.Default && proposed == null)
220 throw new RowNotInTableException("This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.");
222 throw new VersionNotFoundException (Locale.GetText ("There is no " + version.ToString () + " data to access."));
226 internal void SetOriginalValue (string columnName, object val)
228 int columnIndex = _table.Columns.IndexOf (columnName);
229 DataColumn column = _table.Columns[columnIndex];
230 _table.ChangingDataColumn (this, column, val);
232 if (original == null) original = new object [_table.Columns.Count];
233 val = SetColumnValue (val, columnIndex);
234 original[columnIndex] = val;
235 rowState = DataRowState.Modified;
239 /// Gets or sets all of the values for this row through an array.
241 public object[] ItemArray {
244 if (rowState == DataRowState.Detached)
245 throw new RowNotInTableException("This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.");
246 // Accessing deleted rows
247 if (rowState == DataRowState.Deleted)
248 throw new DeletedRowInaccessibleException ("Deleted row information cannot be accessed through the row.");
253 if (value.Length > _table.Columns.Count)
254 throw new ArgumentException ();
256 if (rowState == DataRowState.Deleted)
257 throw new DeletedRowInaccessibleException ();
259 object[] newItems = new object[_table.Columns.Count];
261 for (int i = 0; i < _table.Columns.Count; i++) {
263 if (i < value.Length)
268 newItems[i] = SetColumnValue (v, i);
271 bool orginalEditing = editing;
272 if (!orginalEditing) BeginEdit ();
274 if (!orginalEditing) EndEdit ();
278 private object SetColumnValue (object v, int index)
280 object newval = null;
281 DataColumn col = _table.Columns[index];
283 if (_hasParentCollection && col.ReadOnly && v != this[index])
284 throw new ReadOnlyException ();
288 if (col.DataType.ToString().Equals("System.Guid"))
289 throw new ArgumentException("Cannot set column to be null, Please use DBNull instead");
291 if(col.DefaultValue != DBNull.Value)
293 newval = col.DefaultValue;
295 else if(col.AutoIncrement == true)
297 newval = this [index];
301 if (!col.AllowDBNull)
303 //Constraint violations during data load is raise in DataTable EndLoad
304 this._nullConstraintViolation = true;
305 _nullConstraintMessage = "Column '" + col.ColumnName + "' does not allow nulls.";
308 newval = DBNull.Value;
311 else if (v == DBNull.Value)
314 if (!col.AllowDBNull)
316 //Constraint violations during data load is raise in DataTable EndLoad
317 this._nullConstraintViolation = true;
318 _nullConstraintMessage = "Column '" + col.ColumnName + "' does not allow nulls.";
321 newval = DBNull.Value;
325 Type vType = v.GetType(); // data type of value
326 Type cType = col.DataType; // column data type
329 TypeCode typeCode = Type.GetTypeCode(cType);
331 case TypeCode.Boolean :
332 v = Convert.ToBoolean (v);
335 v = Convert.ToByte (v);
338 v = Convert.ToChar (v);
340 case TypeCode.DateTime :
341 v = Convert.ToDateTime (v);
343 case TypeCode.Decimal :
344 v = Convert.ToDecimal (v);
346 case TypeCode.Double :
347 v = Convert.ToDouble (v);
349 case TypeCode.Int16 :
350 v = Convert.ToInt16 (v);
352 case TypeCode.Int32 :
353 v = Convert.ToInt32 (v);
355 case TypeCode.Int64 :
356 v = Convert.ToInt64 (v);
358 case TypeCode.SByte :
359 v = Convert.ToSByte (v);
361 case TypeCode.Single :
362 v = Convert.ToSingle (v);
364 case TypeCode.String :
365 v = Convert.ToString (v);
367 case TypeCode.UInt16 :
368 v = Convert.ToUInt16 (v);
370 case TypeCode.UInt32 :
371 v = Convert.ToUInt32 (v);
373 case TypeCode.UInt64 :
374 v = Convert.ToUInt64 (v);
378 switch(cType.ToString()) {
379 case "System.TimeSpan" :
380 v = (System.TimeSpan) v;
385 case "System.Object" :
386 //v = (System.Object) v;
390 throw new InvalidCastException("Type not supported.");
398 // The MaxLength property is ignored for non-text columns
399 if ((Type.GetTypeCode(vType) == TypeCode.String) && (this.Table.Columns[index].MaxLength != -1) &&
400 (this.Table.Columns[index].MaxLength < ((string)v).Length)) {
401 throw new ArgumentException("Cannot set column '" + this.Table.Columns[index].ColumnName + "' to '" + v + "'. The value violates the MaxLength limit of this column.");
404 if(col.AutoIncrement == true) {
405 long inc = Convert.ToInt64(v);
406 col.UpdateAutoIncrementValue (inc);
409 col.DataHasBeenSet = true;
414 /// Gets or sets the custom error description for a row.
416 public string RowError {
417 get { return rowError; }
418 set { rowError = value; }
422 /// Gets the current state of the row in regards to its relationship to the
423 /// DataRowCollection.
425 public DataRowState RowState {
426 get { return rowState; }
429 //FIXME?: Couldn't find a way to set the RowState when adding the DataRow
430 //to a Datatable so I added this method. Delete if there is a better way.
431 internal void AttachRow() {
434 rowState = DataRowState.Added;
437 //FIXME?: Couldn't find a way to set the RowState when removing the DataRow
438 //from a Datatable so I added this method. Delete if there is a better way.
439 internal void DetachRow() {
442 _hasParentCollection = false;
443 rowState = DataRowState.Detached;
447 /// Gets the DataTable for which this row has a schema.
449 public DataTable Table {
450 get { return _table; }
454 /// Gets and sets index of row. This is used from
457 internal int XmlRowID {
458 get { return xmlRowID; }
459 set { xmlRowID = value; }
463 /// Gets and sets index of row.
466 get { return _rowId; }
467 set { _rowId = value; }
475 /// Commits all the changes made to this row since the last time AcceptChanges was
478 public void AcceptChanges ()
480 EndEdit(); // in case it hasn't been called
482 case DataRowState.Added:
483 case DataRowState.Modified:
484 rowState = DataRowState.Unchanged;
486 case DataRowState.Deleted:
487 _table.Rows.RemoveInternal (this);
490 case DataRowState.Detached:
491 throw new RowNotInTableException("Cannot perform this operation on a row not in the table.");
493 // Accept from detached
494 if (original == null)
495 original = new object[_table.Columns.Count];
496 Array.Copy (current, original, _table.Columns.Count);
500 /// Begins an edit operation on a DataRow object.
503 public void BeginEdit ()
505 if (rowState == DataRowState.Deleted)
506 throw new DeletedRowInaccessibleException ();
507 if (!HasVersion (DataRowVersion.Proposed)) {
508 proposed = new object[_table.Columns.Count];
509 Array.Copy (current, proposed, current.Length);
511 //TODO: Suspend validation
516 /// Cancels the current edit on the row.
519 public void CancelEdit ()
523 if (HasVersion (DataRowVersion.Proposed)) {
525 if (rowState == DataRowState.Modified)
526 rowState = DataRowState.Unchanged;
531 /// Clears the errors for the row, including the RowError and errors set with
534 public void ClearErrors ()
536 rowError = String.Empty;
537 columnErrors = new String[_table.Columns.Count];
541 /// Deletes the DataRow.
544 public void Delete ()
546 _table.DeletingDataRow(this, DataRowAction.Delete);
548 case DataRowState.Added:
549 // check what to do with child rows
550 CheckChildRows(DataRowAction.Delete);
551 _table.DeleteRowFromIndexes (this);
552 Table.Rows.RemoveInternal (this);
554 // if row was in Added state we move it to Detached.
557 case DataRowState.Deleted:
560 // check what to do with child rows
561 CheckChildRows(DataRowAction.Delete);
562 _table.DeleteRowFromIndexes (this);
563 rowState = DataRowState.Deleted;
566 _table.DeletedDataRow(this, DataRowAction.Delete);
569 // check the child rows of this row before deleting the row.
570 private void CheckChildRows(DataRowAction action)
573 // in this method we find the row that this row is in a relation with them.
574 // in shortly we find all child rows of this row.
575 // then we function according to the DeleteRule of the foriegnkey.
578 // 1. find if this row is attached to dataset.
579 // 2. find if EnforceConstraints is true.
580 // 3. find if there are any constraint on the table that the row is in.
581 if (_table.DataSet != null && _table.DataSet.EnforceConstraints && _table.Constraints.Count > 0)
583 foreach (DataTable table in _table.DataSet.Tables)
585 // loop on all ForeignKeyConstrain of the table.
586 foreach (ForeignKeyConstraint fk in table.Constraints.ForeignKeyConstraints)
588 if (fk.RelatedTable == _table)
591 if (action == DataRowAction.Delete)
592 rule = fk.DeleteRule;
594 rule = fk.UpdateRule;
595 CheckChildRows(fk, action, rule);
602 private void CheckChildRows(ForeignKeyConstraint fkc, DataRowAction action, Rule rule)
604 DataRow[] childRows = GetChildRows(fkc, DataRowVersion.Default);
607 case Rule.Cascade: // delete or change all relted rows.
608 if (childRows != null)
610 for (int j = 0; j < childRows.Length; j++)
612 // if action is delete we delete all child rows
613 if (action == DataRowAction.Delete)
615 if (childRows[j].RowState != DataRowState.Deleted)
616 childRows[j].Delete();
618 // if action is change we change the values in the child row
619 else if (action == DataRowAction.Change)
621 // change only the values in the key columns
622 // set the childcolumn value to the new parent row value
623 for (int k = 0; k < fkc.Columns.Length; k++)
624 childRows[j][fkc.Columns[k]] = this[fkc.RelatedColumns[k], DataRowVersion.Proposed];
629 case Rule.None: // throw an exception if there are any child rows.
630 if (childRows != null)
632 for (int j = 0; j < childRows.Length; j++)
634 if (childRows[j].RowState != DataRowState.Deleted)
636 string changeStr = "Cannot change this row because constraints are enforced on relation " + fkc.ConstraintName +", and changing this row will strand child rows.";
637 string delStr = "Cannot delete this row because constraints are enforced on relation " + fkc.ConstraintName +", and deleting this row will strand child rows.";
638 string message = action == DataRowAction.Delete ? delStr : changeStr;
639 throw new InvalidConstraintException(message);
644 case Rule.SetDefault: // set the values in the child rows to the defult value of the columns.
645 if (childRows != null)
647 for (int j = 0; j < childRows.Length; j++)
649 DataRow child = childRows[j];
650 if (childRows[j].RowState != DataRowState.Deleted)
652 //set only the key columns to default
653 for (int k = 0; k < fkc.Columns.Length; k++)
654 child[fkc.Columns[k]] = fkc.Columns[k].DefaultValue;
659 case Rule.SetNull: // set the values in the child row to null.
660 if (childRows != null)
662 for (int j = 0; j < childRows.Length; j++)
664 DataRow child = childRows[j];
665 if (childRows[j].RowState != DataRowState.Deleted)
667 // set only the key columns to DBNull
668 for (int k = 0; k < fkc.Columns.Length; k++)
669 child.SetNull(fkc.Columns[k]);
679 /// Ends the edit occurring on the row.
682 public void EndEdit ()
684 if (_inChangingEvent)
685 throw new InRowChangingEventException("Cannot call EndEdit inside an OnRowChanging event.");
686 if (rowState == DataRowState.Detached)
692 if (HasVersion (DataRowVersion.Proposed))
694 _inChangingEvent = true;
697 _table.ChangingDataRow(this, DataRowAction.Change);
701 _inChangingEvent = false;
703 if (rowState == DataRowState.Unchanged)
704 rowState = DataRowState.Modified;
706 //Calling next method validates UniqueConstraints
710 if ((_table.DataSet == null || _table.DataSet.EnforceConstraints) && !_table._duringDataLoad)
711 _table.Rows.ValidateDataRowInternal(this);
720 // Now we are going to check all child rows of current row.
721 // In the case the cascade is true the child rows will look up for
722 // parent row. since lookup in index is always on current,
723 // we have to move proposed version of current row to current
724 // in the case of check child row failure we are rolling
725 // current row state back.
726 object[] backup = current;
729 bool editing_backup = editing;
732 // check all child rows.
733 CheckChildRows(DataRowAction.Change);
735 catch (Exception ex) {
736 // if check child rows failed - rollback to previous state
737 // i.e. restore proposed and current versions
740 editing = editing_backup;
741 // since we failed - propagate an exception
744 _table.ChangedDataRow(this, DataRowAction.Change);
749 /// Gets the child rows of this DataRow using the specified DataRelation.
751 public DataRow[] GetChildRows (DataRelation relation)
753 return GetChildRows (relation, DataRowVersion.Current);
757 /// Gets the child rows of a DataRow using the specified RelationName of a
760 public DataRow[] GetChildRows (string relationName)
762 return GetChildRows (Table.DataSet.Relations[relationName]);
766 /// Gets the child rows of a DataRow using the specified DataRelation, and
769 public DataRow[] GetChildRows (DataRelation relation, DataRowVersion version)
771 if (relation == null)
772 return new DataRow[0];
774 if (this.Table == null || RowState == DataRowState.Detached)
775 throw new RowNotInTableException("This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.");
777 if (relation.DataSet != this.Table.DataSet)
778 throw new ArgumentException();
780 if (relation.ChildKeyConstraint != null)
781 return GetChildRows (relation.ChildKeyConstraint, version);
783 ArrayList rows = new ArrayList();
784 DataColumn[] parentColumns = relation.ParentColumns;
785 DataColumn[] childColumns = relation.ChildColumns;
786 int numColumn = parentColumns.Length;
787 if (HasVersion(version))
789 object[] vals = new object[parentColumns.Length];
790 for (int i = 0; i < vals.Length; i++)
791 vals[i] = this[parentColumns[i], version];
793 foreach (DataRow row in relation.ChildTable.Rows)
795 bool allColumnsMatch = false;
796 if (row.HasVersion(DataRowVersion.Default))
798 allColumnsMatch = true;
799 for (int columnCnt = 0; columnCnt < numColumn; ++columnCnt)
801 if (!vals[columnCnt].Equals(
802 row[childColumns[columnCnt], DataRowVersion.Default]))
804 allColumnsMatch = false;
809 if (allColumnsMatch) rows.Add(row);
812 DataRow[] result = relation.ChildTable.NewRowArray(rows.Count);
813 rows.CopyTo(result, 0);
818 /// Gets the child rows of a DataRow using the specified RelationName of a
819 /// DataRelation, and DataRowVersion.
821 public DataRow[] GetChildRows (string relationName, DataRowVersion version)
823 return GetChildRows (Table.DataSet.Relations[relationName], version);
826 private DataRow[] GetChildRows (ForeignKeyConstraint fkc, DataRowVersion version)
828 ArrayList rows = new ArrayList();
829 DataColumn[] parentColumns = fkc.RelatedColumns;
830 DataColumn[] childColumns = fkc.Columns;
831 int numColumn = parentColumns.Length;
832 if (HasVersion(version))
834 object[] vals = new object[parentColumns.Length];
835 for (int i = 0; i < vals.Length; i++)
836 vals[i] = this[parentColumns[i], version];
838 Index index = fkc.Index;
840 // get the child rows from the index
841 Node[] childNodes = index.FindAllSimple (vals);
842 for (int i = 0; i < childNodes.Length; i++) {
843 rows.Add (childNodes[i].Row);
846 else { // if there is no index we search manualy.
847 foreach (DataRow row in fkc.Table.Rows)
849 bool allColumnsMatch = false;
850 if (row.HasVersion(DataRowVersion.Default))
852 allColumnsMatch = true;
853 for (int columnCnt = 0; columnCnt < numColumn; ++columnCnt)
855 if (!vals[columnCnt].Equals(
856 row[childColumns[columnCnt], DataRowVersion.Default]))
858 allColumnsMatch = false;
863 if (allColumnsMatch) rows.Add(row);
868 DataRow[] result = fkc.Table.NewRowArray(rows.Count);
869 rows.CopyTo(result, 0);
874 /// Gets the error description of the specified DataColumn.
876 public string GetColumnError (DataColumn column)
878 return GetColumnError (_table.Columns.IndexOf(column));
882 /// Gets the error description for the column specified by index.
884 public string GetColumnError (int columnIndex)
886 if (columnIndex < 0 || columnIndex >= columnErrors.Length)
887 throw new IndexOutOfRangeException ();
889 string retVal = columnErrors[columnIndex];
891 retVal = string.Empty;
896 /// Gets the error description for the column, specified by name.
898 public string GetColumnError (string columnName)
900 return GetColumnError (_table.Columns.IndexOf(columnName));
904 /// Gets an array of columns that have errors.
906 public DataColumn[] GetColumnsInError ()
908 ArrayList dataColumns = new ArrayList ();
910 for (int i = 0; i < columnErrors.Length; i += 1)
912 if (columnErrors[i] != null && columnErrors[i] != String.Empty)
913 dataColumns.Add (_table.Columns[i]);
916 return (DataColumn[])(dataColumns.ToArray (typeof(DataColumn)));
920 /// Gets the parent row of a DataRow using the specified DataRelation.
922 public DataRow GetParentRow (DataRelation relation)
924 return GetParentRow (relation, DataRowVersion.Current);
928 /// Gets the parent row of a DataRow using the specified RelationName of a
931 public DataRow GetParentRow (string relationName)
933 return GetParentRow (relationName, DataRowVersion.Current);
937 /// Gets the parent row of a DataRow using the specified DataRelation, and
940 public DataRow GetParentRow (DataRelation relation, DataRowVersion version)
942 DataRow[] rows = GetParentRows(relation, version);
943 if (rows.Length == 0) return null;
948 /// Gets the parent row of a DataRow using the specified RelationName of a
949 /// DataRelation, and DataRowVersion.
951 public DataRow GetParentRow (string relationName, DataRowVersion version)
953 return GetParentRow (Table.DataSet.Relations[relationName], version);
957 /// Gets the parent rows of a DataRow using the specified DataRelation.
959 public DataRow[] GetParentRows (DataRelation relation)
961 return GetParentRows (relation, DataRowVersion.Current);
965 /// Gets the parent rows of a DataRow using the specified RelationName of a
968 public DataRow[] GetParentRows (string relationName)
970 return GetParentRows (relationName, DataRowVersion.Current);
974 /// Gets the parent rows of a DataRow using the specified DataRelation, and
977 public DataRow[] GetParentRows (DataRelation relation, DataRowVersion version)
979 // TODO: Caching for better preformance
980 if (relation == null)
981 return new DataRow[0];
983 if (this.Table == null || RowState == DataRowState.Detached)
984 throw new RowNotInTableException("This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.");
986 if (relation.DataSet != this.Table.DataSet)
987 throw new ArgumentException();
989 ArrayList rows = new ArrayList();
990 DataColumn[] parentColumns = relation.ParentColumns;
991 DataColumn[] childColumns = relation.ChildColumns;
992 int numColumn = parentColumns.Length;
993 if (HasVersion(version))
995 object[] vals = new object[childColumns.Length];
996 for (int i = 0; i < vals.Length; i++)
997 vals[i] = this[childColumns[i], version];
999 Index indx = relation.ParentTable.GetIndexByColumns (parentColumns);
1000 if (indx != null) { // get the child rows from the index
1001 Node[] childNodes = indx.FindAllSimple (vals);
1002 for (int i = 0; i < childNodes.Length; i++) {
1003 rows.Add (childNodes[i].Row);
1006 else { // no index so we have to search manualy.
1007 foreach (DataRow row in relation.ParentTable.Rows)
1009 bool allColumnsMatch = false;
1010 if (row.HasVersion(DataRowVersion.Default))
1012 allColumnsMatch = true;
1013 for (int columnCnt = 0; columnCnt < numColumn; columnCnt++)
1015 if (!this[childColumns[columnCnt], version].Equals(
1016 row[parentColumns[columnCnt], DataRowVersion.Default]))
1018 allColumnsMatch = false;
1023 if (allColumnsMatch) rows.Add(row);
1028 DataRow[] result = relation.ParentTable.NewRowArray(rows.Count);
1029 rows.CopyTo(result, 0);
1034 /// Gets the parent rows of a DataRow using the specified RelationName of a
1035 /// DataRelation, and DataRowVersion.
1037 public DataRow[] GetParentRows (string relationName, DataRowVersion version)
1039 return GetParentRows (Table.DataSet.Relations[relationName], version);
1043 /// Gets a value indicating whether a specified version exists.
1045 public bool HasVersion (DataRowVersion version)
1049 case DataRowVersion.Default:
1050 if (rowState == DataRowState.Deleted)
1052 if (rowState == DataRowState.Detached)
1053 return proposed != null;
1055 case DataRowVersion.Proposed:
1056 if (rowState == DataRowState.Deleted)
1058 return (proposed != null);
1059 case DataRowVersion.Current:
1060 if (rowState == DataRowState.Deleted || rowState == DataRowState.Detached)
1062 return (current != null);
1063 case DataRowVersion.Original:
1064 if (rowState == DataRowState.Detached)
1066 return (original != null);
1072 /// Gets a value indicating whether the specified DataColumn contains a null value.
1074 public bool IsNull (DataColumn column)
1076 object o = this[column];
1077 return (o == DBNull.Value);
1081 /// Gets a value indicating whether the column at the specified index contains a null
1084 public bool IsNull (int columnIndex)
1086 object o = this[columnIndex];
1087 return (o == DBNull.Value);
1091 /// Gets a value indicating whether the named column contains a null value.
1093 public bool IsNull (string columnName)
1095 object o = this[columnName];
1096 return (o == DBNull.Value);
1100 /// Gets a value indicating whether the specified DataColumn and DataRowVersion
1101 /// contains a null value.
1103 public bool IsNull (DataColumn column, DataRowVersion version)
1105 object o = this[column, version];
1106 return (o == DBNull.Value);
1110 /// Returns a value indicating whether all of the row columns specified contain a null value.
1112 internal bool IsNullColumns(DataColumn[] columns)
1114 bool allNull = true;
1115 for (int i = 0; i < columns.Length; i++)
1117 if (!IsNull(columns[i]))
1127 /// Rejects all changes made to the row since AcceptChanges was last called.
1129 public void RejectChanges ()
1131 if (RowState == DataRowState.Detached)
1132 throw new RowNotInTableException("This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.");
1133 // If original is null, then nothing has happened since AcceptChanges
1134 // was last called. We have no "original" to go back to.
1135 if (original != null)
1137 Array.Copy (original, current, _table.Columns.Count);
1139 _table.ChangedDataRow (this, DataRowAction.Rollback);
1143 case DataRowState.Added:
1144 _table.DeleteRowFromIndexes (this);
1145 _table.Rows.RemoveInternal (this);
1147 case DataRowState.Modified:
1148 if ((_table.DataSet == null || _table.DataSet.EnforceConstraints) && !_table._duringDataLoad)
1149 _table.Rows.ValidateDataRowInternal(this);
1150 rowState = DataRowState.Unchanged;
1152 case DataRowState.Deleted:
1153 rowState = DataRowState.Unchanged;
1154 if ((_table.DataSet == null || _table.DataSet.EnforceConstraints) && !_table._duringDataLoad)
1155 _table.Rows.ValidateDataRowInternal(this);
1161 // If rows are just loaded via Xml the original values are null.
1162 // So in this case we have to remove all columns.
1163 // FIXME: I'm not realy sure, does this break something else, but
1166 if ((rowState & DataRowState.Added) > 0)
1168 _table.DeleteRowFromIndexes (this);
1169 _table.Rows.RemoveInternal (this);
1170 // if row was in Added state we move it to Detached.
1177 /// Sets the error description for a column specified as a DataColumn.
1179 public void SetColumnError (DataColumn column, string error)
1181 SetColumnError (_table.Columns.IndexOf (column), error);
1185 /// Sets the error description for a column specified by index.
1187 public void SetColumnError (int columnIndex, string error)
1189 if (columnIndex < 0 || columnIndex >= columnErrors.Length)
1190 throw new IndexOutOfRangeException ();
1191 columnErrors[columnIndex] = error;
1195 /// Sets the error description for a column specified by name.
1197 public void SetColumnError (string columnName, string error)
1199 SetColumnError (_table.Columns.IndexOf (columnName), error);
1203 /// Sets the value of the specified DataColumn to a null value.
1205 protected void SetNull (DataColumn column)
1207 this[column] = DBNull.Value;
1211 /// Sets the parent row of a DataRow with specified new parent DataRow.
1213 public void SetParentRow (DataRow parentRow)
1215 SetParentRow(parentRow, null);
1219 /// Sets the parent row of a DataRow with specified new parent DataRow and
1222 public void SetParentRow (DataRow parentRow, DataRelation relation)
1224 if (_table == null || parentRow.Table == null || RowState == DataRowState.Detached)
1225 throw new RowNotInTableException("This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.");
1227 if (parentRow != null && _table.DataSet != parentRow.Table.DataSet)
1228 throw new ArgumentException();
1231 if (relation == null)
1233 foreach (DataRelation parentRel in _table.ParentRelations)
1235 DataColumn[] childCols = parentRel.ChildKeyConstraint.Columns;
1236 DataColumn[] parentCols = parentRel.ChildKeyConstraint.RelatedColumns;
1238 for (int i = 0; i < parentCols.Length; i++)
1240 if (parentRow == null)
1241 this[childCols[i].Ordinal] = DBNull.Value;
1243 this[childCols[i].Ordinal] = parentRow[parentCols[i]];
1250 DataColumn[] childCols = relation.ChildKeyConstraint.Columns;
1251 DataColumn[] parentCols = relation.ChildKeyConstraint.RelatedColumns;
1253 for (int i = 0; i < parentCols.Length; i++)
1255 if (parentRow == null)
1256 this[childCols[i].Ordinal] = DBNull.Value;
1258 this[childCols[i].Ordinal] = parentRow[parentCols[i]];
1264 //Copy all values of this DataaRow to the row parameter.
1265 internal void CopyValuesToRow(DataRow row)
1269 throw new ArgumentNullException("row");
1271 throw new ArgumentException("'row' is the same as this object");
1273 DataColumnCollection columns = Table.Columns;
1275 for(int i = 0; i < columns.Count; i++){
1277 string columnName = columns[i].ColumnName;
1278 int index = row.Table.Columns.IndexOf(columnName);
1279 //if a column with the same name exists in both rows copy the values
1281 if (HasVersion(DataRowVersion.Original))
1283 if (row.original == null)
1284 row.original = new object[row.Table.Columns.Count];
1285 row.original[index] = row.SetColumnValue(original[i], index);
1287 if (HasVersion(DataRowVersion.Current))
1289 if (row.current == null)
1290 row.current = new object[row.Table.Columns.Count];
1291 row.current[index] = row.SetColumnValue(current[i], index);
1293 if (HasVersion(DataRowVersion.Proposed))
1295 if (row.proposed == null)
1296 row.proposed = new object[row.Table.Columns.Count];
1297 row.proposed[index] = row.SetColumnValue(proposed[i], index);
1300 //Saving the current value as the column value
1301 row[index] = row.current[index];
1306 row.rowState = RowState;
1307 row.RowError = RowError;
1308 row.columnErrors = columnErrors;
1312 public void CollectionChanged(object sender, System.ComponentModel.CollectionChangeEventArgs args)
1314 // if a column is added we hava to add an additional value the
1315 // the priginal, current and propoed arrays.
1316 // this scenario can happened in merge operation.
1318 if (args.Action == System.ComponentModel.CollectionChangeAction.Add)
1321 if (current != null)
1323 tmp = new object[current.Length + 1];
1324 Array.Copy (current, tmp, current.Length);
1325 tmp[tmp.Length - 1] = SetColumnValue(null, tmp.Length - 1);
1328 if (proposed != null)
1330 tmp = new object[proposed.Length + 1];
1331 Array.Copy (proposed, tmp, proposed.Length);
1332 tmp[tmp.Length - 1] = SetColumnValue(null, tmp.Length - 1);
1335 if(original != null)
1337 tmp = new object[original.Length + 1];
1338 Array.Copy (original, tmp, original.Length);
1339 tmp[tmp.Length - 1] = SetColumnValue(null, tmp.Length - 1);
1346 internal bool IsRowChanged(DataRowState rowState) {
1347 if((RowState & rowState) != 0)
1350 //we need to find if child rows of this row changed.
1351 //if yes - we should return true
1353 // if the rowState is deleted we should get the original version of the row
1354 // else - we should get the current version of the row.
1355 DataRowVersion version = (rowState == DataRowState.Deleted) ? DataRowVersion.Original : DataRowVersion.Current;
1356 int count = Table.ChildRelations.Count;
1357 for (int i = 0; i < count; i++){
1358 DataRelation rel = Table.ChildRelations[i];
1359 DataRow[] childRows = GetChildRows(rel, version);
1360 for (int j = 0; j < childRows.Length; j++){
1361 if (childRows[j].IsRowChanged(rowState))
1369 internal bool HasParentCollection
1373 return _hasParentCollection;
1377 _hasParentCollection = value;
1381 internal void CheckNullConstraints()
1383 if (_nullConstraintViolation)
1385 for (int i = 0; i < proposed.Length; i++)
1387 if (this[i] == DBNull.Value && !_table.Columns[i].AllowDBNull)
1388 throw new NoNullAllowedException(_nullConstraintMessage);
1390 _nullConstraintViolation = false;
1394 #endregion // Methods