Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between DataTable.Clear and DataTable.Rows.Clear?

Tags:

.net

I recall there is a difference between some methods/properties called directly on the DataTable class, and the identically named methods/properties on the DataTable.Rows property. (Might have been the RowCount/Count property for which I read this.) The difference is one of them disregards DataRow.RowState, and the other respects/uses it.

In this particular case I'm wondering about the difference between DataTable.Clear and DataTable.Rows.Clear. I can imagine one of them actually removes all rows, and the other one just marks them as deleted.

So my question is, is there a difference between the two Clear methods, and if so what is the difference?

(Oh, this is for .NET 1.1 btw, in case the semantics changed from one version to another.)

like image 957
Tobi Avatar asked Oct 03 '08 09:10

Tobi


2 Answers

In .Net 1.1, DataRowCollection.Clear calls DataTable.Clear

However, in .Net 2.0, there is a difference. If I understand the source correctly, DataTable.Clear will clear unattached rows (created using DataTable.NewRow) whereas DataRowCollection.Clear won't.

The difference is in RecordManager.Clear (source below, from the .Net Reference Source for v3.5 SP 0); clearAll is true only when called from DataTable.Clear.

    internal void Clear(bool clearAll) { 
        if (clearAll) {
            for(int record = 0; record < recordCapacity; ++record) { 
                rows[record] = null;
            }
            int count = table.columnCollection.Count;
            for(int i = 0; i < count; ++i) { 
                //

                DataColumn column = table.columnCollection[i]; 
                for(int record = 0; record < recordCapacity; ++record) {
                    column.FreeRecord(record); 
                }
            }
            lastFreeRecord = 0;
            freeRecordList.Clear(); 
        }
        else { // just clear attached rows 
            freeRecordList.Capacity = freeRecordList.Count + table.Rows.Count; 
            for(int record = 0; record < recordCapacity; ++record) {
                if (rows[record]!= null && rows[record].rowID != -1) { 
                    int tempRecord = record;
                    FreeRecord(ref tempRecord);
                }
            } 
        }
    } 
like image 200
SLaks Avatar answered Oct 08 '22 00:10

SLaks


I've been testing the different methods now in .NET 1.1/VS2003, seems Matt Hamilton is right.

  • DataTable.Clear and DataTable.Rows.Clear seem to behave identical with respect to the two things I tested: both remove all rows (they don't mark them as deleted, they really remove them from the table), and neither removes the columns of the table.
  • DataTable.Reset clears rows and columns.
  • DataTable.Rows.Count does include deleted rows. (This might be 1.1 specific)
  • foreach iterates over deleted rows. (I'm pretty sure deleted rows are skipped in 2.0.)
like image 32
Tobi Avatar answered Oct 08 '22 00:10

Tobi