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 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
19 // Permission is hereby granted, free of charge, to any person obtaining
20 // a copy of this software and associated documentation files (the
21 // "Software"), to deal in the Software without restriction, including
22 // without limitation the rights to use, copy, modify, merge, publish,
23 // distribute, sublicense, and/or sell copies of the Software, and to
24 // permit persons to whom the Software is furnished to do so, subject to
25 // the following conditions:
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 using System.Collections;
41 using System.Globalization;
44 namespace System.Data {
46 /// Represents a row of data in a DataTable.
53 private DataTable _table;
55 internal int _original = -1;
56 internal int _current = -1;
57 internal int _proposed = -1;
59 private ArrayList _columnErrors;
60 private string rowError;
61 private DataRowState rowState;
62 internal int xmlRowID = 0;
63 internal bool _nullConstraintViolation;
64 private string _nullConstraintMessage;
65 private bool editing = false;
66 private bool _hasParentCollection;
67 private bool _inChangingEvent;
70 private XmlDataDocument.XmlDataElement mappedElement;
71 internal bool _inExpressionEvaluation = false;
78 /// This member supports the .NET Framework infrastructure and is not intended to be
79 /// used directly from your code.
81 protected internal DataRow (DataRowBuilder builder)
83 _table = builder.Table;
84 // Get the row id from the builder.
85 _rowId = builder._rowId;
87 _proposed = _table.RecordCache.NewRecord();
88 // Initialise the data columns of the row with the dafault values, if any
89 // TODO : should proposed version be available immediately after record creation ?
90 foreach(DataColumn column in _table.Columns) {
91 column.DataContainer.CopyValue(_table.DefaultValuesRowIndex,_proposed);
94 rowError = String.Empty;
96 //on first creating a DataRow it is always detached.
97 rowState = DataRowState.Detached;
99 ArrayList aiColumns = _table.Columns.AutoIncrmentColumns;
100 foreach (DataColumn dc in aiColumns) {
101 this [dc] = dc.AutoIncrementValue();
104 // create mapped XmlDataElement
105 DataSet ds = _table.DataSet;
106 if (ds != null && ds._xmlDataDocument != null)
107 mappedElement = new XmlDataDocument.XmlDataElement (this, _table.Prefix, _table.TableName, _table.Namespace, ds._xmlDataDocument);
110 internal DataRow(DataTable table,int rowId)
116 #endregion // Constructors
120 private ArrayList ColumnErrors
123 if (_columnErrors == null) {
124 _columnErrors = new ArrayList();
126 return _columnErrors;
130 _columnErrors = value;
135 /// Gets a value indicating whether there are errors in a row.
137 public bool HasErrors {
139 if (RowError != string.Empty)
142 foreach(String columnError in ColumnErrors) {
143 if (columnError != null && columnError != string.Empty) {
152 /// Gets or sets the data stored in the column specified by name.
154 public object this[string columnName] {
155 get { return this[columnName, DataRowVersion.Default]; }
157 int columnIndex = _table.Columns.IndexOf (columnName);
158 if (columnIndex == -1)
159 throw new IndexOutOfRangeException ();
160 this[columnIndex] = value;
165 /// Gets or sets the data stored in specified DataColumn
167 public object this[DataColumn column] {
170 return this[column, DataRowVersion.Default];}
172 int columnIndex = _table.Columns.IndexOf (column);
173 if (columnIndex == -1)
174 throw new ArgumentException ("The column does not belong to this table.");
175 this[columnIndex] = value;
180 /// Gets or sets the data stored in column specified by index.
182 public object this[int columnIndex] {
183 get { return this[columnIndex, DataRowVersion.Default]; }
185 if (columnIndex < 0 || columnIndex > _table.Columns.Count)
186 throw new IndexOutOfRangeException ();
187 if (rowState == DataRowState.Deleted)
188 throw new DeletedRowInaccessibleException ();
190 DataColumn column = _table.Columns[columnIndex];
191 _table.ChangingDataColumn (this, column, value);
193 if (value == null && column.DataType != typeof(string)) {
194 throw new ArgumentException("Cannot set column " + column.ColumnName + " to be null, Please use DBNull instead");
197 CheckValue (value, column);
199 bool orginalEditing = editing;
200 if (!orginalEditing) {
204 column[_proposed] = value;
205 _table.ChangedDataColumn (this, column, value);
206 if (!orginalEditing) {
213 /// Gets the specified version of data stored in the named column.
215 public object this[string columnName, DataRowVersion version] {
217 int columnIndex = _table.Columns.IndexOf (columnName);
218 if (columnIndex == -1)
219 throw new IndexOutOfRangeException ();
220 return this[columnIndex, version];
225 /// Gets the specified version of data stored in the specified DataColumn.
227 public object this[DataColumn column, DataRowVersion version] {
229 if (column.Table != Table)
230 throw new ArgumentException ("The column does not belong to this table.");
231 int columnIndex = column.Ordinal;
232 return this[columnIndex, version];
237 /// Gets the data stored in the column, specified by index and version of the data to
240 public object this[int columnIndex, DataRowVersion version] {
242 if (columnIndex < 0 || columnIndex > _table.Columns.Count)
243 throw new IndexOutOfRangeException ();
244 // Accessing deleted rows
245 if (!_inExpressionEvaluation && rowState == DataRowState.Deleted && version != DataRowVersion.Original)
246 throw new DeletedRowInaccessibleException ("Deleted row information cannot be accessed through the row.");
248 DataColumn column = _table.Columns[columnIndex];
249 if (column.Expression != String.Empty) {
250 object o = column.CompiledExpression.Eval (this);
251 return Convert.ChangeType (o, column.DataType);
254 int recordIndex = IndexFromVersion(version);
256 if (recordIndex >= 0) {
257 return column[recordIndex];
260 if (rowState == DataRowState.Detached && version == DataRowVersion.Default && _proposed < 0)
261 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.");
263 throw new VersionNotFoundException (Locale.GetText ("There is no " + version.ToString () + " data to access."));
268 /// Gets or sets all of the values for this row through an array.
270 public object[] ItemArray {
273 if (rowState == DataRowState.Detached)
274 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.");
275 // Accessing deleted rows
276 if (rowState == DataRowState.Deleted)
277 throw new DeletedRowInaccessibleException ("Deleted row information cannot be accessed through the row.");
279 object[] items = new object[_table.Columns.Count];
280 foreach(DataColumn column in _table.Columns) {
281 items[column.Ordinal] = column[_current];
286 if (value.Length > _table.Columns.Count)
287 throw new ArgumentException ();
289 if (rowState == DataRowState.Deleted)
290 throw new DeletedRowInaccessibleException ();
292 bool orginalEditing = editing;
293 if (!orginalEditing) {
296 object newVal = null;
297 DataColumnChangeEventArgs e = new DataColumnChangeEventArgs();
298 foreach(DataColumn column in _table.Columns) {
299 int i = column.Ordinal;
300 newVal = (i < value.Length) ? value[i] : null;
302 e.Initialize(this, column, newVal);
303 _table.RaiseOnColumnChanged(e);
304 CheckValue (e.ProposedValue, column);
305 column[_proposed] = e.ProposedValue;
307 if (!orginalEditing) {
314 /// Gets the current state of the row in regards to its relationship to the
315 /// DataRowCollection.
317 public DataRowState RowState {
324 /// Gets the DataTable for which this row has a schema.
326 public DataTable Table {
333 /// Gets and sets index of row. This is used from
336 internal int XmlRowID {
346 /// Gets and sets index of row.
361 //FIXME?: Couldn't find a way to set the RowState when adding the DataRow
362 //to a Datatable so I added this method. Delete if there is a better way.
363 internal void AttachRow() {
365 Table.RecordCache.DisposeRecord(_current);
367 _current = _proposed;
369 rowState = DataRowState.Added;
372 //FIXME?: Couldn't find a way to set the RowState when removing the DataRow
373 //from a Datatable so I added this method. Delete if there is a better way.
374 internal void DetachRow() {
375 if (_proposed >= 0) {
376 _table.RecordCache.DisposeRecord(_proposed);
380 _hasParentCollection = false;
381 rowState = DataRowState.Detached;
384 private void CheckValue (object v, DataColumn col)
386 if (_hasParentCollection && col.ReadOnly) {
387 throw new ReadOnlyException ();
390 if (v == null || v == DBNull.Value) {
391 if (col.AllowDBNull || col.AutoIncrement || col.DefaultValue != DBNull.Value) {
395 //Constraint violations during data load is raise in DataTable EndLoad
396 this._nullConstraintViolation = true;
397 if (this.Table._duringDataLoad) {
398 this.Table._nullConstraintViolationDuringDataLoad = true;
400 _nullConstraintMessage = "Column '" + col.ColumnName + "' does not allow nulls.";
405 internal void SetValuesFromDataRecord(IDataRecord record, int[] mapping)
407 if ( mapping.Length > Table.Columns.Count)
408 throw new ArgumentException ();
410 // bool orginalEditing = editing;
411 // if (!orginalEditing) {
415 if (!HasVersion(DataRowVersion.Proposed)) {
416 _proposed = Table.RecordCache.NewRecord();
420 for(int i=0; i < mapping.Length; i++) {
421 DataColumn column = Table.Columns[i];
422 column.DataContainer.SetItemFromDataRecord(_proposed, record,mapping[i]);
423 if ( column.AutoIncrement ) {
424 column.UpdateAutoIncrementValue(column.DataContainer.GetInt64(_proposed));
429 Table.RecordCache.DisposeRecord(_proposed);
434 // if (!orginalEditing) {
440 /// Gets or sets the custom error description for a row.
442 public string RowError {
451 internal int IndexFromVersion(DataRowVersion version)
453 if (HasVersion(version))
457 case DataRowVersion.Default:
458 if (editing || rowState == DataRowState.Detached) {
459 recordIndex = _proposed;
462 recordIndex = _current;
465 case DataRowVersion.Proposed:
466 recordIndex = _proposed;
468 case DataRowVersion.Current:
469 recordIndex = _current;
471 case DataRowVersion.Original:
472 recordIndex = _original;
475 throw new ArgumentException ();
482 internal XmlDataDocument.XmlDataElement DataElement {
483 get { return mappedElement; }
484 set { mappedElement = value; }
487 internal void SetOriginalValue (string columnName, object val)
489 DataColumn column = _table.Columns[columnName];
490 _table.ChangingDataColumn (this, column, val);
492 if (_original < 0 || _original == _current) {
493 // really add a row cache, if _original is not there &
495 _original = Table.RecordCache.NewRecord();
497 CheckValue (val, column);
498 column[_original] = val;
499 rowState = DataRowState.Modified;
503 /// Commits all the changes made to this row since the last time AcceptChanges was
506 public void AcceptChanges ()
508 EndEdit(); // in case it hasn't been called
510 case DataRowState.Unchanged:
512 case DataRowState.Added:
513 case DataRowState.Modified:
514 rowState = DataRowState.Unchanged;
516 case DataRowState.Deleted:
517 _table.Rows.RemoveInternal (this);
520 case DataRowState.Detached:
521 throw new RowNotInTableException("Cannot perform this operation on a row not in the table.");
523 // Accept from detached
524 if (_original >= 0) {
525 Table.RecordCache.DisposeRecord(_original);
527 _original = _current;
531 /// Begins an edit operation on a DataRow object.
533 public void BeginEdit ()
535 if (_inChangingEvent)
536 throw new InRowChangingEventException("Cannot call BeginEdit inside an OnRowChanging event.");
537 if (rowState == DataRowState.Deleted)
538 throw new DeletedRowInaccessibleException ();
539 if (!HasVersion (DataRowVersion.Proposed)) {
540 _proposed = Table.RecordCache.NewRecord();
541 foreach(DataColumn column in Table.Columns) {
542 column.DataContainer.CopyValue(_current,_proposed);
545 // setting editing to true stops validations on the row
550 /// Cancels the current edit on the row.
552 public void CancelEdit ()
554 if (_inChangingEvent)
555 throw new InRowChangingEventException("Cannot call CancelEdit inside an OnRowChanging event.");
557 if (HasVersion (DataRowVersion.Proposed)) {
558 Table.RecordCache.DisposeRecord(_proposed);
560 if (rowState == DataRowState.Modified) {
561 rowState = DataRowState.Unchanged;
567 /// Clears the errors for the row, including the RowError and errors set with
570 public void ClearErrors ()
572 rowError = String.Empty;
573 ColumnErrors.Clear();
577 /// Deletes the DataRow.
579 public void Delete ()
581 _table.DeletingDataRow(this, DataRowAction.Delete);
583 case DataRowState.Added:
584 // check what to do with child rows
585 CheckChildRows(DataRowAction.Delete);
586 _table.DeleteRowFromIndexes (this);
587 Table.Rows.RemoveInternal (this);
589 // if row was in Added state we move it to Detached.
592 case DataRowState.Deleted:
595 // check what to do with child rows
596 CheckChildRows(DataRowAction.Delete);
597 _table.DeleteRowFromIndexes (this);
598 rowState = DataRowState.Deleted;
601 _table.DeletedDataRow(this, DataRowAction.Delete);
604 // check the child rows of this row before deleting the row.
605 private void CheckChildRows(DataRowAction action)
608 // in this method we find the row that this row is in a relation with them.
609 // in shortly we find all child rows of this row.
610 // then we function according to the DeleteRule of the foriegnkey.
613 // 1. find if this row is attached to dataset.
614 // 2. find if EnforceConstraints is true.
615 // 3. find if there are any constraint on the table that the row is in.
616 if (_table.DataSet != null && _table.DataSet.EnforceConstraints && _table.Constraints.Count > 0)
618 foreach (DataTable table in _table.DataSet.Tables)
620 // loop on all ForeignKeyConstrain of the table.
621 foreach (ForeignKeyConstraint fk in table.Constraints.ForeignKeyConstraints)
623 if (fk.RelatedTable == _table)
626 if (action == DataRowAction.Delete)
627 rule = fk.DeleteRule;
629 rule = fk.UpdateRule;
630 CheckChildRows(fk, action, rule);
637 private void CheckChildRows(ForeignKeyConstraint fkc, DataRowAction action, Rule rule)
639 DataRow[] childRows = GetChildRows(fkc, DataRowVersion.Default);
642 case Rule.Cascade: // delete or change all relted rows.
643 if (childRows != null)
645 for (int j = 0; j < childRows.Length; j++)
647 // if action is delete we delete all child rows
648 if (action == DataRowAction.Delete)
650 if (childRows[j].RowState != DataRowState.Deleted)
651 childRows[j].Delete();
653 // if action is change we change the values in the child row
654 else if (action == DataRowAction.Change)
656 // change only the values in the key columns
657 // set the childcolumn value to the new parent row value
658 for (int k = 0; k < fkc.Columns.Length; k++)
659 childRows[j][fkc.Columns[k]] = this[fkc.RelatedColumns[k], DataRowVersion.Proposed];
664 case Rule.None: // throw an exception if there are any child rows.
665 if (childRows != null)
667 for (int j = 0; j < childRows.Length; j++)
669 if (childRows[j].RowState != DataRowState.Deleted)
671 string changeStr = "Cannot change this row because constraints are enforced on relation " + fkc.ConstraintName +", and changing this row will strand child rows.";
672 string delStr = "Cannot delete this row because constraints are enforced on relation " + fkc.ConstraintName +", and deleting this row will strand child rows.";
673 string message = action == DataRowAction.Delete ? delStr : changeStr;
674 throw new InvalidConstraintException(message);
679 case Rule.SetDefault: // set the values in the child rows to the defult value of the columns.
680 if (childRows != null && childRows.Length > 0) {
681 int defaultValuesRowIndex = childRows[0].Table.DefaultValuesRowIndex;
682 foreach(DataRow childRow in childRows) {
683 if (childRow.RowState != DataRowState.Deleted) {
684 int defaultIdx = childRow.IndexFromVersion(DataRowVersion.Default);
685 foreach(DataColumn column in fkc.Columns) {
686 column.DataContainer.CopyValue(defaultValuesRowIndex,defaultIdx);
692 case Rule.SetNull: // set the values in the child row to null.
693 if (childRows != null)
695 for (int j = 0; j < childRows.Length; j++)
697 DataRow child = childRows[j];
698 if (childRows[j].RowState != DataRowState.Deleted)
700 // set only the key columns to DBNull
701 for (int k = 0; k < fkc.Columns.Length; k++)
702 child.SetNull(fkc.Columns[k]);
712 /// Ends the edit occurring on the row.
714 public void EndEdit ()
716 if (_inChangingEvent)
717 throw new InRowChangingEventException("Cannot call EndEdit inside an OnRowChanging event.");
718 if (rowState == DataRowState.Detached)
724 CheckReadOnlyStatus();
725 if (HasVersion (DataRowVersion.Proposed))
727 _inChangingEvent = true;
730 _table.ChangingDataRow(this, DataRowAction.Change);
734 _inChangingEvent = false;
736 if (rowState == DataRowState.Unchanged)
737 rowState = DataRowState.Modified;
739 //Calling next method validates UniqueConstraints
743 if ((_table.DataSet == null || _table.DataSet.EnforceConstraints) && !_table._duringDataLoad)
744 _table.Rows.ValidateDataRowInternal(this);
749 Table.RecordCache.DisposeRecord(_proposed);
754 // Now we are going to check all child rows of current row.
755 // In the case the cascade is true the child rows will look up for
756 // parent row. since lookup in index is always on current,
757 // we have to move proposed version of current row to current
758 // in the case of check child row failure we are rolling
759 // current row state back.
760 int backup = _current;
761 _current = _proposed;
762 bool editing_backup = editing;
765 // check all child rows.
766 CheckChildRows(DataRowAction.Change);
768 if (_original != backup) {
769 Table.RecordCache.DisposeRecord(backup);
772 catch (Exception ex) {
773 // if check child rows failed - rollback to previous state
774 // i.e. restore proposed and current versions
775 _proposed = _current;
777 editing = editing_backup;
778 // since we failed - propagate an exception
781 _table.ChangedDataRow(this, DataRowAction.Change);
786 /// Gets the child rows of this DataRow using the specified DataRelation.
788 public DataRow[] GetChildRows (DataRelation relation)
790 return GetChildRows (relation, DataRowVersion.Current);
794 /// Gets the child rows of a DataRow using the specified RelationName of a
797 public DataRow[] GetChildRows (string relationName)
799 return GetChildRows (Table.DataSet.Relations[relationName]);
803 /// Gets the child rows of a DataRow using the specified DataRelation, and
806 public DataRow[] GetChildRows (DataRelation relation, DataRowVersion version)
808 if (relation == null)
809 return new DataRow[0];
811 if (this.Table == null || RowState == DataRowState.Detached)
812 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.");
814 if (relation.DataSet != this.Table.DataSet)
815 throw new ArgumentException();
817 if (relation.ChildKeyConstraint != null)
818 return GetChildRows (relation.ChildKeyConstraint, version);
820 ArrayList rows = new ArrayList();
821 DataColumn[] parentColumns = relation.ParentColumns;
822 DataColumn[] childColumns = relation.ChildColumns;
823 int numColumn = parentColumns.Length;
824 if (HasVersion(version))
826 object[] vals = new object[parentColumns.Length];
827 for (int i = 0; i < vals.Length; i++)
828 vals[i] = this[parentColumns[i], version];
830 foreach (DataRow row in relation.ChildTable.Rows)
832 bool allColumnsMatch = false;
833 if (row.HasVersion(DataRowVersion.Default))
835 allColumnsMatch = true;
836 for (int columnCnt = 0; columnCnt < numColumn; ++columnCnt)
838 if (!vals[columnCnt].Equals(
839 row[childColumns[columnCnt], DataRowVersion.Default]))
841 allColumnsMatch = false;
846 if (allColumnsMatch) rows.Add(row);
849 DataRow[] result = relation.ChildTable.NewRowArray(rows.Count);
850 rows.CopyTo(result, 0);
855 /// Gets the child rows of a DataRow using the specified RelationName of a
856 /// DataRelation, and DataRowVersion.
858 public DataRow[] GetChildRows (string relationName, DataRowVersion version)
860 return GetChildRows (Table.DataSet.Relations[relationName], version);
863 private DataRow[] GetChildRows (ForeignKeyConstraint fkc, DataRowVersion version)
865 ArrayList rows = new ArrayList();
866 DataColumn[] parentColumns = fkc.RelatedColumns;
867 DataColumn[] childColumns = fkc.Columns;
868 int numColumn = parentColumns.Length;
869 if (HasVersion(version)) {
870 Index index = fkc.Index;
872 // get the child rows from the index
873 Node[] childNodes = index.FindAllSimple (parentColumns, IndexFromVersion(version));
874 for (int i = 0; i < childNodes.Length; i++) {
875 rows.Add (childNodes[i].Row);
878 else { // if there is no index we search manualy.
879 int curIndex = IndexFromVersion(DataRowVersion.Current);
880 int tmpRecord = fkc.Table.RecordCache.NewRecord();
883 for (int i = 0; i < numColumn; i++) {
884 // according to MSDN: the DataType value for both columns must be identical.
885 childColumns[i].DataContainer.CopyValue(parentColumns[i].DataContainer, curIndex, tmpRecord);
888 foreach (DataRow row in fkc.Table.Rows) {
889 bool allColumnsMatch = false;
890 if (row.HasVersion(DataRowVersion.Default)) {
891 allColumnsMatch = true;
892 int childIndex = row.IndexFromVersion(DataRowVersion.Default);
893 for (int columnCnt = 0; columnCnt < numColumn; ++columnCnt) {
894 if (childColumns[columnCnt].DataContainer.CompareValues(childIndex, tmpRecord) != 0) {
895 allColumnsMatch = false;
900 if (allColumnsMatch) {
906 fkc.Table.RecordCache.DisposeRecord(tmpRecord);
911 DataRow[] result = fkc.Table.NewRowArray(rows.Count);
912 rows.CopyTo(result, 0);
917 /// Gets the error description of the specified DataColumn.
919 public string GetColumnError (DataColumn column)
921 return GetColumnError (_table.Columns.IndexOf(column));
925 /// Gets the error description for the column specified by index.
927 public string GetColumnError (int columnIndex)
929 if (columnIndex < 0 || columnIndex >= Table.Columns.Count)
930 throw new IndexOutOfRangeException ();
932 string retVal = null;
933 if (columnIndex < ColumnErrors.Count) {
934 retVal = (String) ColumnErrors[columnIndex];
936 return (retVal != null) ? retVal : String.Empty;
940 /// Gets the error description for the column, specified by name.
942 public string GetColumnError (string columnName)
944 return GetColumnError (_table.Columns.IndexOf(columnName));
948 /// Gets an array of columns that have errors.
950 public DataColumn[] GetColumnsInError ()
952 ArrayList dataColumns = new ArrayList ();
954 int columnOrdinal = 0;
955 foreach(String columnError in ColumnErrors) {
956 if (columnError != null && columnError != String.Empty) {
957 dataColumns.Add (_table.Columns[columnOrdinal]);
962 return (DataColumn[])(dataColumns.ToArray (typeof(DataColumn)));
966 /// Gets the parent row of a DataRow using the specified DataRelation.
968 public DataRow GetParentRow (DataRelation relation)
970 return GetParentRow (relation, DataRowVersion.Current);
974 /// Gets the parent row of a DataRow using the specified RelationName of a
977 public DataRow GetParentRow (string relationName)
979 return GetParentRow (relationName, DataRowVersion.Current);
983 /// Gets the parent row of a DataRow using the specified DataRelation, and
986 public DataRow GetParentRow (DataRelation relation, DataRowVersion version)
988 DataRow[] rows = GetParentRows(relation, version);
989 if (rows.Length == 0) return null;
994 /// Gets the parent row of a DataRow using the specified RelationName of a
995 /// DataRelation, and DataRowVersion.
997 public DataRow GetParentRow (string relationName, DataRowVersion version)
999 return GetParentRow (Table.DataSet.Relations[relationName], version);
1003 /// Gets the parent rows of a DataRow using the specified DataRelation.
1005 public DataRow[] GetParentRows (DataRelation relation)
1007 return GetParentRows (relation, DataRowVersion.Current);
1011 /// Gets the parent rows of a DataRow using the specified RelationName of a
1014 public DataRow[] GetParentRows (string relationName)
1016 return GetParentRows (relationName, DataRowVersion.Current);
1020 /// Gets the parent rows of a DataRow using the specified DataRelation, and
1023 public DataRow[] GetParentRows (DataRelation relation, DataRowVersion version)
1025 // TODO: Caching for better preformance
1026 if (relation == null)
1027 return new DataRow[0];
1029 if (this.Table == null || RowState == DataRowState.Detached)
1030 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.");
1032 if (relation.DataSet != this.Table.DataSet)
1033 throw new ArgumentException();
1035 ArrayList rows = new ArrayList();
1036 DataColumn[] parentColumns = relation.ParentColumns;
1037 DataColumn[] childColumns = relation.ChildColumns;
1038 int numColumn = parentColumns.Length;
1039 if (HasVersion(version)) {
1040 Index indx = relation.ParentTable.GetIndexByColumns (parentColumns);
1042 (Table == null || Table.DataSet == null ||
1043 Table.DataSet.EnforceConstraints)) { // get the child rows from the index
1044 Node[] childNodes = indx.FindAllSimple(childColumns, IndexFromVersion(version));
1045 for (int i = 0; i < childNodes.Length; i++) {
1046 rows.Add (childNodes[i].Row);
1049 else { // no index so we have to search manualy.
1050 int curIndex = IndexFromVersion(DataRowVersion.Current);
1051 int tmpRecord = relation.ParentTable.RecordCache.NewRecord();
1054 for (int i = 0; i < numColumn; i++) {
1055 // according to MSDN: the DataType value for both columns must be identical.
1056 parentColumns[i].DataContainer.CopyValue(childColumns[i].DataContainer, curIndex, tmpRecord);
1059 foreach (DataRow row in relation.ParentTable.Rows) {
1060 bool allColumnsMatch = false;
1061 if (row.HasVersion(DataRowVersion.Default)) {
1062 allColumnsMatch = true;
1063 int parentIndex = row.IndexFromVersion(DataRowVersion.Default);
1064 for (int columnCnt = 0; columnCnt < numColumn; columnCnt++) {
1065 if (parentColumns[columnCnt].DataContainer.CompareValues(parentIndex, tmpRecord) != 0) {
1066 allColumnsMatch = false;
1071 if (allColumnsMatch) {
1077 relation.ParentTable.RecordCache.DisposeRecord(tmpRecord);
1082 DataRow[] result = relation.ParentTable.NewRowArray(rows.Count);
1083 rows.CopyTo(result, 0);
1088 /// Gets the parent rows of a DataRow using the specified RelationName of a
1089 /// DataRelation, and DataRowVersion.
1091 public DataRow[] GetParentRows (string relationName, DataRowVersion version)
1093 return GetParentRows (Table.DataSet.Relations[relationName], version);
1097 /// Gets a value indicating whether a specified version exists.
1099 public bool HasVersion (DataRowVersion version)
1102 case DataRowVersion.Default:
1103 if (rowState == DataRowState.Deleted && !_inExpressionEvaluation)
1105 if (rowState == DataRowState.Detached)
1106 return _proposed >= 0;
1108 case DataRowVersion.Proposed:
1109 if (rowState == DataRowState.Deleted && !_inExpressionEvaluation)
1111 return _proposed >= 0;
1112 case DataRowVersion.Current:
1113 if ((rowState == DataRowState.Deleted && !_inExpressionEvaluation) || rowState == DataRowState.Detached)
1115 return _current >= 0;
1116 case DataRowVersion.Original:
1117 if (rowState == DataRowState.Detached)
1119 return _original >= 0;
1125 /// Gets a value indicating whether the specified DataColumn contains a null value.
1127 public bool IsNull (DataColumn column)
1129 return IsNull(column, DataRowVersion.Default);
1133 /// Gets a value indicating whether the column at the specified index contains a null
1136 public bool IsNull (int columnIndex)
1138 return IsNull(Table.Columns[columnIndex]);
1142 /// Gets a value indicating whether the named column contains a null value.
1144 public bool IsNull (string columnName)
1146 return IsNull(Table.Columns[columnName]);
1150 /// Gets a value indicating whether the specified DataColumn and DataRowVersion
1151 /// contains a null value.
1153 public bool IsNull (DataColumn column, DataRowVersion version)
1155 return column.DataContainer.IsNull(IndexFromVersion(version));
1159 /// Returns a value indicating whether all of the row columns specified contain a null value.
1161 internal bool IsNullColumns(DataColumn[] columns)
1163 bool allNull = true;
1164 for (int i = 0; i < columns.Length; i++)
1166 if (!IsNull(columns[i]))
1176 /// Rejects all changes made to the row since AcceptChanges was last called.
1178 public void RejectChanges ()
1180 if (RowState == DataRowState.Detached)
1181 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.");
1182 // If original is null, then nothing has happened since AcceptChanges
1183 // was last called. We have no "original" to go back to.
1184 if (HasVersion(DataRowVersion.Original)) {
1185 if (_current >= 0 ) {
1186 Table.RecordCache.DisposeRecord(_current);
1188 _current = _original;
1190 _table.ChangedDataRow (this, DataRowAction.Rollback);
1194 case DataRowState.Added:
1195 _table.DeleteRowFromIndexes (this);
1196 _table.Rows.RemoveInternal (this);
1198 case DataRowState.Modified:
1199 if ((_table.DataSet == null || _table.DataSet.EnforceConstraints) && !_table._duringDataLoad)
1200 _table.Rows.ValidateDataRowInternal(this);
1201 rowState = DataRowState.Unchanged;
1203 case DataRowState.Deleted:
1204 rowState = DataRowState.Unchanged;
1205 if ((_table.DataSet == null || _table.DataSet.EnforceConstraints) && !_table._duringDataLoad)
1206 _table.Rows.ValidateDataRowInternal(this);
1212 // If rows are just loaded via Xml the original values are null.
1213 // So in this case we have to remove all columns.
1214 // FIXME: I'm not realy sure, does this break something else, but
1217 if ((rowState & DataRowState.Added) > 0)
1219 _table.DeleteRowFromIndexes (this);
1220 _table.Rows.RemoveInternal (this);
1221 // if row was in Added state we move it to Detached.
1228 /// Sets the error description for a column specified as a DataColumn.
1230 public void SetColumnError (DataColumn column, string error)
1232 SetColumnError (_table.Columns.IndexOf (column), error);
1236 /// Sets the error description for a column specified by index.
1238 public void SetColumnError (int columnIndex, string error)
1240 if (columnIndex < 0 || columnIndex >= Table.Columns.Count)
1241 throw new IndexOutOfRangeException ();
1243 while(ColumnErrors.Count < columnIndex) {
1244 ColumnErrors.Add(null);
1246 ColumnErrors.Add(error);
1250 /// Sets the error description for a column specified by name.
1252 public void SetColumnError (string columnName, string error)
1254 SetColumnError (_table.Columns.IndexOf (columnName), error);
1258 /// Sets the value of the specified DataColumn to a null value.
1260 protected void SetNull (DataColumn column)
1262 this[column] = DBNull.Value;
1266 /// Sets the parent row of a DataRow with specified new parent DataRow.
1268 public void SetParentRow (DataRow parentRow)
1270 SetParentRow(parentRow, null);
1274 /// Sets the parent row of a DataRow with specified new parent DataRow and
1277 public void SetParentRow (DataRow parentRow, DataRelation relation)
1279 if (_table == null || parentRow.Table == null || RowState == DataRowState.Detached)
1280 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.");
1282 if (parentRow != null && _table.DataSet != parentRow.Table.DataSet)
1283 throw new ArgumentException();
1286 if (relation == null)
1288 foreach (DataRelation parentRel in _table.ParentRelations)
1290 DataColumn[] childCols = parentRel.ChildKeyConstraint.Columns;
1291 DataColumn[] parentCols = parentRel.ChildKeyConstraint.RelatedColumns;
1293 for (int i = 0; i < parentCols.Length; i++)
1295 if (parentRow == null)
1296 this[childCols[i].Ordinal] = DBNull.Value;
1298 this[childCols[i].Ordinal] = parentRow[parentCols[i]];
1305 DataColumn[] childCols = relation.ChildKeyConstraint.Columns;
1306 DataColumn[] parentCols = relation.ChildKeyConstraint.RelatedColumns;
1308 for (int i = 0; i < parentCols.Length; i++)
1310 if (parentRow == null)
1311 this[childCols[i].Ordinal] = DBNull.Value;
1313 this[childCols[i].Ordinal] = parentRow[parentCols[i]];
1319 //Copy all values of this DataaRow to the row parameter.
1320 internal void CopyValuesToRow(DataRow row)
1323 throw new ArgumentNullException("row");
1325 throw new ArgumentException("'row' is the same as this object");
1327 foreach(DataColumn column in Table.Columns) {
1328 DataColumn targetColumn = row.Table.Columns[column.ColumnName];
1329 //if a column with the same name exists in both rows copy the values
1330 if(targetColumn != null) {
1331 int index = targetColumn.Ordinal;
1332 if (HasVersion(DataRowVersion.Original)) {
1333 if (row._original < 0) {
1334 row._original = row.Table.RecordCache.NewRecord();
1336 object val = column[_original];
1337 row.CheckValue(val, targetColumn);
1338 targetColumn[row._original] = val;
1340 if (HasVersion(DataRowVersion.Current)) {
1341 if (row._current < 0) {
1342 row._current = row.Table.RecordCache.NewRecord();
1344 object val = column[_current];
1345 row.CheckValue(val, targetColumn);
1346 targetColumn[row._current] = val;
1348 if (HasVersion(DataRowVersion.Proposed)) {
1349 if (row._proposed < 0) {
1350 row._proposed = row.Table.RecordCache.NewRecord();
1352 object val = column[row._proposed];
1353 row.CheckValue(val, targetColumn);
1354 targetColumn[row._proposed] = val;
1357 //Saving the current value as the column value
1358 row[index] = targetColumn[row._current];
1365 // Copy row state - rowState and errors
1366 internal void CopyState(DataRow row)
1368 row.rowState = RowState;
1369 row.RowError = RowError;
1370 row.ColumnErrors = (ArrayList)ColumnErrors.Clone();
1373 internal bool IsRowChanged(DataRowState rowState) {
1374 if((RowState & rowState) != 0)
1377 //we need to find if child rows of this row changed.
1378 //if yes - we should return true
1380 // if the rowState is deleted we should get the original version of the row
1381 // else - we should get the current version of the row.
1382 DataRowVersion version = (rowState == DataRowState.Deleted) ? DataRowVersion.Original : DataRowVersion.Current;
1383 int count = Table.ChildRelations.Count;
1384 for (int i = 0; i < count; i++){
1385 DataRelation rel = Table.ChildRelations[i];
1386 DataRow[] childRows = GetChildRows(rel, version);
1387 for (int j = 0; j < childRows.Length; j++){
1388 if (childRows[j].IsRowChanged(rowState))
1396 internal bool HasParentCollection
1400 return _hasParentCollection;
1404 _hasParentCollection = value;
1408 internal void CheckNullConstraints()
1410 if (_nullConstraintViolation) {
1411 if (HasVersion(DataRowVersion.Proposed)) {
1412 foreach(DataColumn column in Table.Columns) {
1413 if (IsNull(column) && !column.AllowDBNull) {
1414 throw new NoNullAllowedException(_nullConstraintMessage);
1418 _nullConstraintViolation = false;
1422 internal void CheckReadOnlyStatus()
1424 if (HasVersion(DataRowVersion.Proposed)) {
1425 int defaultIdx = IndexFromVersion(DataRowVersion.Default);
1426 foreach(DataColumn column in Table.Columns) {
1427 if ((column.DataContainer.CompareValues(defaultIdx,_proposed) != 0) && column.ReadOnly) {
1428 throw new ReadOnlyException();
1434 #endregion // Methods