我有一个 DataTable 可能有更多列。但是“NetAmount”、“TotalAmount”、“Destination”是始终存在于 DataTable 中的 DataTable 列。
这里我想从 DataTable 中删除“NetAmount”、“TotalAmount”和“Destination”这三列,并取 DataTable 中的其他列值。
我尝试如下所示并获得所需的输出。
dtAttribute.Columns.Remove("NetAmount"); //dtAttribute is the Main DataTable
dtAttribute.Columns.Remove("TotalAmount");
dtAttribute.Columns.Remove("Destination");
DataTable dtItem = dtAttribute.Copy();
但是看起来很幼稚,很冗长。还有其他方法吗?请提出建议。
最佳答案
您的代码没有任何问题(除了您在 删除列之后复制表格——您确定这是您想要的吗?)。
如果你想要更抽象的东西(而不是一次又一次地重复同一行),你可以考虑在循环中删除列:
var dtItem = dtAttribute.Copy(); // if you want to keep a copy of the original table
var toRemove = new string[] {"NetAmount", "TotalAmount", "Destination"};
foreach (col in toRemove)
dtItem.Columns.Remove(col);
关于c# - 如何使用 C#.Net 删除更多 DataTable 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6775845/