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

后端 未结 7 1565
天涯浪人
天涯浪人 2021-01-18 06:51

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 pro

相关标签:
7条回答
  • 2021-01-18 07:24

    I don't believe that DataTable.Clear does clear 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);
    
    0 讨论(0)
  • 2021-01-18 07:25

    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);
                    }
                } 
            }
        } 
    
    0 讨论(0)
  • 2021-01-18 07:31

    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.)
    0 讨论(0)
  • 2021-01-18 07:31

    The both do the same thing. One is just an inherited method from the Collections class. And the Table.Clear() just calls that method.

    0 讨论(0)
  • 2021-01-18 07:35

    Do Below and Its working absolutely fine....

    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 讨论(0)
  • 2021-01-18 07:44

    AFAIK, the main difference between datatable.clear and datatable.rows.clear, is that datatable.clear clears both rows and columns. So 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 right back to the beginning.

    datatable.reset is effectively the next level up from datatable.clear. Using datatable.clear will fail if there are any constraints applied that would be violated, but using datatable.reset will get rid of anything and everything that has been put in place since the datatable was created.

    0 讨论(0)
提交回复
热议问题