Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7858189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:18:36+00:00 2026-06-02T21:18:36+00:00

DataTable.Rows.Add() adds a row to the data table. However, how does it handle the

  • 0

DataTable.Rows.Add() adds a row to the data table. However, how does it handle the underlying array?

When adding a single row at a time, does it rebuild the entire array with each row added?

Or is it able to simply modify the existing array without any hit on performance?

I am wondering if it’s better to determine the size of the array before filling it with data, or if somehow the data table is able to modify the collection without (behind the scenes) copying and moving things.

It’s my understanding that to adjust an array you have to redefine it and move previously existing data into the new structure.

My question is what is the work flow for the Collection.Add() method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:18:37+00:00Added an answer on June 2, 2026 at 9:18 pm

    Take a look using software like DotPeek:

    DataTable.Rows.Add(DataRow row)
    {
        this.table.AddRow(row, -1);
    }
    

    which calls:

    DataTable.AddRow(DataRow row, int proposedID)
    {
        this.InsertRow(row, proposedID, -1);
    }
    

    which calls:

    DataTable.InsertRow(DataRow row, int proposedID, int pos)
    {
        this.InsertRow(row, (long) proposedID, pos, true);
    }  
    

    which calls:

    DataTable.InsertRow(DataRow row, long proposedID, int pos, bool fireEvent)
    {
        Exception deferredException = (Exception) null;
        if (row == null)
            throw ExceptionBuilder.ArgumentNull("row");
        if (row.Table != this)
            throw ExceptionBuilder.RowAlreadyInOtherCollection();
        if (row.rowID != -1L)
            throw ExceptionBuilder.RowAlreadyInTheCollection();
        row.BeginEdit();
        int proposedRecord = row.tempRecord;
        row.tempRecord = -1;
        if (proposedID == -1L)
            proposedID = this.nextRowID;
        bool flag;
        if (flag = this.nextRowID <= proposedID)
            this.nextRowID = checked (proposedID + 1L);
        try
        {
            try
            {
                row.rowID = proposedID;
                this.SetNewRecordWorker(row, proposedRecord, DataRowAction.Add, false, false, pos, fireEvent, out deferredException);
            }
            catch
            {
                if (flag && this.nextRowID == proposedID + 1L)
                    this.nextRowID = proposedID;
                row.rowID = -1L;
                row.tempRecord = proposedRecord;
                throw;
            }
            if (deferredException != null)
                throw deferredException;
            if (!this.EnforceConstraints || this.inLoad)
                return;
            int count = this.columnCollection.Count;
            for (int index = 0; index < count; ++index)
            {
                DataColumn dataColumn = this.columnCollection[index];
                if (dataColumn.Computed)
                    dataColumn.CheckColumnConstraint(row, DataRowAction.Add);
            }
        }
        finally
        {
            row.ResetLastChangedColumn();
        }
    }
    

    which calls:

    DataTable.SetNewRecordWorker(DataRow row, int proposedRecord, DataRowAction action, bool isInMerge, bool suppressEnsurePropertyChanged, int position, bool fireEvent, out Exception deferredException)
    {
        deferredException = (Exception) null;
        if (row.tempRecord != proposedRecord)
        {
        if (!this.inDataLoad)
        {
            row.CheckInTable();
            this.CheckNotModifying(row);
        }
        if (proposedRecord == row.newRecord)
        {
            if (!isInMerge)
            return;
            this.RaiseRowChanged((DataRowChangeEventArgs) null, row, action);
            return;
        }
        else
            row.tempRecord = proposedRecord;
        }
        DataRowChangeEventArgs args = (DataRowChangeEventArgs) null;
        try
        {
        row._action = action;
        args = this.RaiseRowChanging((DataRowChangeEventArgs) null, row, action, fireEvent);
        }
        catch
        {
        row.tempRecord = -1;
        throw;
        }
        finally
        {
        row._action = DataRowAction.Nothing;
        }
        row.tempRecord = -1;
        int record = row.newRecord;
        int num = proposedRecord != -1 ? proposedRecord : (row.RowState != DataRowState.Unchanged ? row.oldRecord : -1);
        if (action == DataRowAction.Add)
        {
        if (position == -1)
            this.Rows.ArrayAdd(row);
        else
            this.Rows.ArrayInsert(row, position);
        }
        List<DataRow> cachedRows = (List<DataRow>) null;
        if ((action == DataRowAction.Delete || action == DataRowAction.Change) && (this.dependentColumns != null && this.dependentColumns.Count > 0))
        {
        cachedRows = new List<DataRow>();
        for (int index = 0; index < this.ParentRelations.Count; ++index)
        {
            DataRelation relation = this.ParentRelations[index];
            if (relation.ChildTable == row.Table)
            cachedRows.InsertRange(cachedRows.Count, (IEnumerable<DataRow>) row.GetParentRows(relation));
        }
        for (int index = 0; index < this.ChildRelations.Count; ++index)
        {
            DataRelation relation = this.ChildRelations[index];
            if (relation.ParentTable == row.Table)
            cachedRows.InsertRange(cachedRows.Count, (IEnumerable<DataRow>) row.GetChildRows(relation));
        }
        }
        if (!suppressEnsurePropertyChanged && !row.HasPropertyChanged && (row.newRecord != proposedRecord && -1 != proposedRecord) && -1 != row.newRecord)
        {
        row.LastChangedColumn = (DataColumn) null;
        row.LastChangedColumn = (DataColumn) null;
        }
        if (this.LiveIndexes.Count != 0)
        {
        if (-1 == record && -1 != proposedRecord && (-1 != row.oldRecord && proposedRecord != row.oldRecord))
            record = row.oldRecord;
        DataViewRowState recordState1 = row.GetRecordState(record);
        DataViewRowState recordState2 = row.GetRecordState(num);
        row.newRecord = proposedRecord;
        if (proposedRecord != -1)
            this.recordManager[proposedRecord] = row;
        DataViewRowState recordState3 = row.GetRecordState(record);
        DataViewRowState recordState4 = row.GetRecordState(num);
        this.RecordStateChanged(record, recordState1, recordState3, num, recordState2, recordState4);
        }
        else
        {
        row.newRecord = proposedRecord;
        if (proposedRecord != -1)
            this.recordManager[proposedRecord] = row;
        }
        row.ResetLastChangedColumn();
        if (-1 != record && record != row.oldRecord && (record != row.tempRecord && record != row.newRecord) && row == this.recordManager[record])
        this.FreeRecord(ref record);
        if (row.RowState == DataRowState.Detached && row.rowID != -1L)
        this.RemoveRow(row, false);
        if (this.dependentColumns != null)
        {
        if (this.dependentColumns.Count > 0)
        {
            try
            {
            this.EvaluateExpressions(row, action, cachedRows);
            }
            catch (Exception ex)
            {
            if (action != DataRowAction.Add)
                throw ex;
            deferredException = ex;
            }
        }
        }
        try
        {
        if (!fireEvent)
            return;
        this.RaiseRowChanged(args, row, action);
        }
        catch (Exception ex)
        {
        if (!ADP.IsCatchableExceptionType(ex))
            throw;
        else
            ExceptionBuilder.TraceExceptionWithoutRethrow(ex);
        }
    }
    

    which calls one of those:

    DataRowCollection.ArrayAdd(DataRow row)
    {
      row.RBTreeNodeId = this.list.Add(row);
    }
    
    DataRowCollection.ArrayInsert(DataRow row, int pos)
    {
      row.RBTreeNodeId = this.list.Insert(pos, row);
    }
    

    this.list is of type DataRowCollection.DataRowTree, derived from RBTree<DataRow>.

    private sealed class DataRowTree : RBTree<DataRow>
    

    RBTree<DataRow> and RBTreeNodeId allows us to conclude that a Red-Black tree is being used!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if add row to DataTable DataRow row = datatable1.NewRow(); row[column2]=column2; row[column6]=column6; datatable1.Rows.Add(row); How about
The Add method for a DataTable contains an overload for adding data to a
foreach (DataRow row in dt.Rows) { string[] temprow={,,,row[3].ToString()}; dtworklist.Rows.Add(temprow); } dt is my datatable.
I have the below data table DataTable dtEquityHoldings = new DataTable(EquityHoldings); dtEquityHoldings.Columns.Add(EquityHoldings); dtEquityHoldings.Columns.Add(PortfolioWeight(%)); dtEquityHoldings.Columns.Add(MarketValue,
I have an table. I need to iterate, for each(datarow r in datatable.rows){ foreach(datacolumns
I have a datatable that contains rows of transaction data for multiple users. Each
I have datatable with column name tag and 100 rows of data.I need to
ds = (DataSet)Session[Details]; DataTable dt = ds.Tables[0]; DataTable temp = dt.Clone(); dt.Rows.Add(ds.Tables[0].Select(ID = +
I've VB.NET function that reads data from an Excel sheet and adds rows in
Using the following DataTable: Dim d As New DataTable() d.Columns.Add(Product, GetType(System.String)) d.Columns.Add(Value, GetType(System.Double)) d.Rows.Add(New

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.