.net DataTable.Clear 和 DataTable.Rows.Clear 之间有区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/166159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a difference between DataTable.Clear and DataTable.Rows.Clear?
提问by Tobi
I recall there is a difference between some methods/properties called directly on the DataTableclass, and the identically named methods/properties on the DataTable.Rowsproperty. (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.
我记得有一些方法/属性直接呼吁之间的差异的DataTable类,并在该相同名称的方法/属性DataTable.Rows财产。(可能是我读到的 RowCount/Count 属性。)不同之处在于其中一个忽略DataRow.RowState,而另一个则尊重/使用它。
In this particular case I'm wondering about the difference between DataTable.Clearand DataTable.Rows.Clear. I can imagine one of them actually removes all rows, and the other one just marks them as deleted.
在这种特殊情况下,我想知道DataTable.Clear和DataTable.Rows.Clear之间的区别。我可以想象其中一个实际上删除了所有行,而另一个只是将它们标记为已删除。
So my question is, is there a difference between the two Clear methods, and if so what is the difference?
所以我的问题是,这两种 Clear 方法是否有区别,如果有,有什么区别?
(Oh, this is for .NET 1.1 btw, in case the semantics changed from one version to another.)
(哦,这是 .NET 1.1 顺便说一句,以防语义从一个版本更改为另一个版本。)
回答by SLaks
In .Net 1.1, DataRowCollection.Clearcalls DataTable.Clear
在 .Net 1.1 中,DataRowCollection.Clear调用DataTable.Clear
However, in .Net 2.0, there is a difference.
If I understand the source correctly, DataTable.Clearwill clear unattached rows (created using DataTable.NewRow) whereas DataRowCollection.Clear won't.
但是,在 .Net 2.0 中,存在差异。如果我正确理解源代码,DataTable.Clear将清除未附加的行(使用 创建DataTable.NewRow)而 DataRowCollection.Clear 不会。
The difference is in RecordManager.Clear(source below, from the .Net Reference Sourcefor v3.5 SP 0); clearAllis true only when called from DataTable.Clear.
区别在于RecordManager.Clear(来源如下,来自v3.5 SP 0的.Net 参考源);clearAll仅当从 调用时才为真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);
}
}
}
}
回答by Tobi
I've been testing the different methods now in .NET 1.1/VS2003, seems Matt Hamilton is right.
我现在一直在 .NET 1.1/VS2003 中测试不同的方法,似乎 Matt Hamilton 是对的。
- 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.)
- DataTable.Clear 和 DataTable.Rows.Clear 似乎在我测试的两件事方面表现相同:都删除所有行(他们没有将它们标记为已删除,他们确实将它们从表中删除),并且都不删除列表的。
- DataTable.Reset 清除行和列。
- DataTable.Rows.Count 确实包括已删除的行。(这可能是 1.1 特定的)
- foreach 遍历已删除的行。(我很确定删除的行在 2.0 中被跳过。)
回答by Jaymz
AFAIK, the main difference between datatable.clearand datatable.rows.clear, is that datatable.clearclears 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.resetto go right back to the beginning.
据我所知,之间的主要区别datatable.clear和datatable.rows.clear,是datatable.clear清除行和列。因此,如果您想保留表结构(即列),请使用datatable.rows.clear. 如果您想从头开始,请使用datatable.clear,甚至datatable.reset直接回到起点。
datatable.resetis effectively the next level up from datatable.clear. Using datatable.clearwill fail if there are any constraints applied that would be violated, but using datatable.resetwill get rid of anything and everything that has been put in place since the datatable was created.
datatable.reset实际上是从datatable.clear. datatable.clear如果应用了任何会被违反的约束,Using将失败,但 usingdatatable.reset将摆脱自创建数据表以来已经放置的任何东西。
回答by Matt Hamilton
I don't believe that DataTable.Clear does clear columns. This code writes "1" to standard output:
我不相信 DataTable.Clear 会清除列。此代码将“1”写入标准输出:
var d = new DataTable();
d.Columns.Add("Hello", typeof(string));
d.Clear();
Console.WriteLine(d.Columns.Count);
回答by Richard Szalay
There is no difference between them. DataRowCollection.Clear() calls Table.Clear()
它们之间没有区别。DataRowCollection.Clear() 调用 Table.Clear()
Table.Clear() checks that the table can be cleared (constraints can prevent this), removes the rows and rebuilds any indexes.
Table.Clear() 检查表是否可以被清除(约束可以防止这种情况发生),删除行并重建任何索引。
回答by Himalaya Garg
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();
回答by Joe Chin
The both do the same thing. One is just an inherited method from the Collections class. And the Table.Clear() just calls that method.
两者都做同样的事情。一个只是从 Collections 类继承的方法。而 Table.Clear() 只是调用该方法。

