2004-05-14 Umadevi S <sumadevi@novell.com>
[mono.git] / mcs / class / System.Data / System.Data / DataRow.cs
index 7e0f3d8ecd0e41cbad7a2e6649effccbc4efd0be..fe0869907486b8a9370bb3edf6d71b8e206099e8 100644 (file)
@@ -16,6 +16,7 @@
 using System;
 using System.Collections;
 using System.Globalization;
+using System.Xml;
 
 namespace System.Data {
        /// <summary>
@@ -44,6 +45,8 @@ namespace System.Data {
                private int _rowId;
                internal bool _inExpressionEvaluation = false;
 
+               private XmlDataDocument.XmlDataElement mappedElement;
+
                #endregion
 
                #region Constructors
@@ -82,6 +85,11 @@ namespace System.Data {
                                this [dc] = dc.AutoIncrementValue();
                        }
                        _table.Columns.CollectionChanged += new System.ComponentModel.CollectionChangeEventHandler(CollectionChanged);
+
+                       // create mapped XmlDataElement
+                       DataSet ds = _table.DataSet;
+                       if (ds != null && ds._xmlDataDocument != null)
+                               mappedElement = new XmlDataDocument.XmlDataElement (this, _table.Prefix, _table.TableName, _table.Namespace, ds._xmlDataDocument);
                }
 
                
@@ -294,8 +302,9 @@ namespace System.Data {
                                }
                                else if(col.AutoIncrement == true && CanAccess(index,DataRowVersion.Default)) 
                                {
+                                       // AutoIncrement column is already filled,
+                                       // so it just need to return existing value.
                                        newval = this [index];
-//                                     newval = col.AutoIncrementValue ();
                                }
                                else 
                                {
@@ -457,6 +466,11 @@ namespace System.Data {
                        get { return _table; }
                }
 
+               internal XmlDataDocument.XmlDataElement DataElement {
+                       get { return mappedElement; }
+                       set { mappedElement = value; }
+               }
+
                /// <summary>
                /// Gets and sets index of row. This is used from 
                /// XmlDataDocument.
@@ -1283,26 +1297,27 @@ namespace System.Data {
                        for(int i = 0; i < columns.Count; i++){
 
                                string columnName = columns[i].ColumnName;
-                               int index = row.Table.Columns.IndexOf(columnName);
+                               DataColumn column = row.Table.Columns[columnName];
                                //if a column with the same name exists in both rows copy the values
-                               if(index != -1) {
+                               if(column != null) {
+                                       int index = column.Ordinal;
                                        if (HasVersion(DataRowVersion.Original))
                                        {
                                                if (row.original == null)
                                                        row.original = new object[row.Table.Columns.Count];
-                                               row.original[index] = row.SetColumnValue(original[i], Table.Columns[index], index);
+                                               row.original[index] = row.SetColumnValue(original[i], column, index);
                                        }
                                        if (HasVersion(DataRowVersion.Current))
                                        {
                                                if (row.current == null)
                                                        row.current = new object[row.Table.Columns.Count];
-                                               row.current[index] = row.SetColumnValue(current[i], Table.Columns[index], index);
+                                               row.current[index] = row.SetColumnValue(current[i], column, index);
                                        }
                                        if (HasVersion(DataRowVersion.Proposed))
                                        {
                                                if (row.proposed == null)
                                                        row.proposed = new object[row.Table.Columns.Count];
-                                               row.proposed[index] = row.SetColumnValue(proposed[i], Table.Columns[index], index);
+                                               row.proposed[index] = row.SetColumnValue(proposed[i], column, index);
                                        }
                                        
                                        //Saving the current value as the column value
@@ -1317,7 +1332,7 @@ namespace System.Data {
                }
 
                
-               public void CollectionChanged(object sender, System.ComponentModel.CollectionChangeEventArgs args)
+               private void CollectionChanged(object sender, System.ComponentModel.CollectionChangeEventArgs args)
                {
                        // if a column is added we hava to add an additional value the 
                        // the priginal, current and propoed arrays.
@@ -1329,21 +1344,21 @@ namespace System.Data {
                                int index = this.Table.Columns.Count - 1;
                                if (current != null)
                                {
-                                       tmp = new object[current.Length + 1];
+                                       tmp = new object [index + 1];
                                        Array.Copy (current, tmp, current.Length);
                                        tmp[tmp.Length - 1] = SetColumnValue(null, this.Table.Columns[index], index);
                                        current = tmp;
                                }
                                if (proposed != null)
                                {
-                                       tmp = new object[proposed.Length + 1];
+                                       tmp = new object [index + 1];
                                        Array.Copy (proposed, tmp, proposed.Length);
                                        tmp[tmp.Length - 1] = SetColumnValue(null, this.Table.Columns[index], index);
                                        proposed = tmp;
                                }
                                if(original != null)
                                {
-                                       tmp = new object[original.Length + 1];
+                                       tmp = new object [index + 1];
                                        Array.Copy (original, tmp, original.Length);
                                        tmp[tmp.Length - 1] = SetColumnValue(null, this.Table.Columns[index], index);
                                        original = tmp;
@@ -1478,9 +1493,9 @@ namespace System.Data {
                        if (_nullConstraintViolation) {
                                if (proposed != null) {
                                        for (int i = 0; i < proposed.Length; i++) {
-                                       if (this[i] == DBNull.Value && !_table.Columns[i].AllowDBNull)
-                                               throw new NoNullAllowedException(_nullConstraintMessage);
-                               }
+                                               if (this[i] == DBNull.Value && !_table.Columns[i].AllowDBNull)
+                                                       throw new NoNullAllowedException(_nullConstraintMessage);
+                                       }
                                }
                                _nullConstraintViolation = false;
                        }