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

I remember that there is a difference between some methods / properties that are called directly by the DataTable class and identically named methods / properties on the DataTable.Rows property. (Perhaps this is the RowCount / Count property, for which I read it.) The difference is that one of them ignores DataRow.RowState , and the other aspects / uses it.

In this particular case, I’m interested in learning about the difference between DataTable.Clear and DataTable.Rows.Clear . I can imagine that one of them actually deletes all the lines, and the other 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 if the semantics have changed from one version to another.)

+6
source share
7 answers

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

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

The difference lies in RecordManager.Clear (source below, from the .NET Reference Source for v3.5 SP 0); clearAll 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); } } } } 
+7
source share

I tested various methods in .NET 1.1 / VS2003, it seems that Matt Hamilton is right.

  • DataTable.Clear and DataTable.Rows.Clear seem to behave the same with respect to the two things I tested: both delete all rows (they do not mark them as deleted, they really delete them from the table) and neither remove the columns of the table.
  • DataTable.Reset clears rows and columns.
  • DataTable.Rows.Count includes deleted rows. (This may be a value of 1.1)
  • foreach iterates over deleted lines. (I am sure that deleted lines are skipped in version 2.0.)
+4
source share

AFAIK, the main difference between datatable.clear and datatable.rows.clear is that datatable.clear clears both rows and columns. Therefore, if you want to keep the table structure (i.e. Columns), use datatable.rows.clear . And if you want to start from scratch, use datatable.clear or even datatable.reset to go back to the beginning.

datatable.reset is actually the next level above datatable.clear . Using datatable.clear will fail if any restrictions are applied that are violated, but using datatable.reset get rid of anything and everything that has been created since the datatable was created.

+2
source share

I do not believe DataTable.Clear clears columns. This code writes "1" to standard output:

 var d = new DataTable(); d.Columns.Add("Hello", typeof(string)); d.Clear(); Console.WriteLine(d.Columns.Count); 
+2
source share

There is no difference between them. Call DataRowCollection.Clear () Table.Clear ()

Table.Clear () checks if the table can be cleared (restrictions can prevent this), deletes rows and restores any indexes.

0
source share

Do it below, and his work is absolutely wonderful ....

 DataRow[] d_row = dt_result.Select("isfor_report='True'"); DataTable dt = dt_result.Clone(); foreach (DataRow dr in d_row) { dt.ImportRow(dr); } gv_view_result.DataSource = dt; gv_view_result.DataBind(); 
0
source share

Both are doing the same. One of them is just an inherited method from the Collections class. And Table.Clear () just calls this method.

-2
source share

All Articles