System.Data.DataTable.InsertRow C# (CSharp) Method

InsertRow() private method

private InsertRow ( DataRow row, long proposedID ) : void
row DataRow
proposedID long
return void
        internal void InsertRow(DataRow row, long proposedID)
        {
            long logScopeId = DataCommonEventSource.Log.EnterScope("<ds.DataTable.InsertRow|INFO> {0}, row={1}", ObjectID, row._objectID);
            try
            {
                if (row.Table != this)
                {
                    throw ExceptionBuilder.RowAlreadyInOtherCollection();
                }
                if (row.rowID != -1)
                {
                    throw ExceptionBuilder.RowAlreadyInTheCollection();
                }
                if (row._oldRecord == -1 && row._newRecord == -1)
                {
                    throw ExceptionBuilder.RowEmpty();
                }

                if (proposedID == -1)
                {
                    proposedID = _nextRowID;
                }

                row.rowID = proposedID;
                if (_nextRowID <= proposedID)
                {
                    _nextRowID = checked(proposedID + 1);
                }

                DataRowChangeEventArgs drcevent = null;

                if (row._newRecord != -1)
                {
                    row._tempRecord = row._newRecord;
                    row._newRecord = -1;

                    try
                    {
                        drcevent = RaiseRowChanging(null, row, DataRowAction.Add, true);
                    }
                    catch
                    {
                        row._tempRecord = -1;
                        throw;
                    }

                    row._newRecord = row._tempRecord;
                    row._tempRecord = -1;
                }

                if (row._oldRecord != -1)
                {
                    _recordManager[row._oldRecord] = row;
                }

                if (row._newRecord != -1)
                {
                    _recordManager[row._newRecord] = row;
                }

                Rows.ArrayAdd(row);

                if (row.RowState == DataRowState.Unchanged)
                {
                    //  how about row.oldRecord == row.newRecord both == -1
                    RecordStateChanged(row._oldRecord, DataViewRowState.None, DataViewRowState.Unchanged);
                }
                else
                {
                    RecordStateChanged(row._oldRecord, DataViewRowState.None, row.GetRecordState(row._oldRecord),
                                       row._newRecord, DataViewRowState.None, row.GetRecordState(row._newRecord));
                }

                if (_dependentColumns != null && _dependentColumns.Count > 0)
                {
                    EvaluateExpressions(row, DataRowAction.Add, null);
                }

                RaiseRowChanged(drcevent, row, DataRowAction.Add);
            }
            finally
            {
                DataCommonEventSource.Log.ExitScope(logScopeId);
            }
        }

Same methods

DataTable::InsertRow ( DataRow row, int proposedID, int pos ) : void
DataTable::InsertRow ( DataRow row, long proposedID, int pos, bool fireEvent ) : void

Usage Example

Example #1
0
 public void InsertAt(DataRow row, int pos)
 {
     if (pos < 0)
     {
         throw ExceptionBuilder.RowInsertOutOfRange(pos);
     }
     if (pos >= list.Count)
     {
         table.AddRow(row, -1);
     }
     else
     {
         table.InsertRow(row, -1, pos);
     }
 }
DataTable