错误:列***已经属于另一个数据表

问题描述:

我尝试过滤一些数据并添加到另一个数据表。虽然运行时间错误是显示错误:列***已经属于另一个数据表

Column *** already belongs to another dataTable 

这里我的代码:

Private Function FilterbyMode(ByVal FullTable As DataTable, ByVal Mode As String) As DataTable 
    Dim Result As New DataTable(), i As Integer, j As Integer 
    Try 
     i = 0 
     j = 0 
     With FullTable 
      Result.Clear() 
      Result.Columns.Clear() 
      For i = 0 To .Columns.Count - 1 
       Result.Columns.Add(.Columns(i)) 
      Next 
      For i = 0 To .Rows.Count - 1 
       If .Rows(i)(5).ToString = Mode Then 
        Result.Rows.Add(.Rows(i)) 
       End If 
      Next 
     End With 
    Catch ex As Exception 
     lobjGeneral.Err_Handler("Reports", "LoadGrid", Err.Description, "Reports.aspx") 
    End Try 
    FilterbyMode = Result 
End Function 

什么是我的问题?任何一个给我的解决方案..

+0

您可以使用** Clone()**方法制作其他表格,然后使用** ImportRow()**方法处理您所关注的行 – V4Vendetta 2011-04-18 07:17:36

你可以尝试像

Result = FullTable.Clone(); //replicate the structure 

然后

If .Rows(i)(5).ToString == Mode Then // i belive you need comparison 
     Result.ImportRow(.Rows(i)); // Add this row 
End If 

你不能老是从DataTable添加到其他DataTable添加列,因为它是否会被裁判复制并且如果您将更改一个数据表中的列,它将在另一个数据表中更改...
您可以执行的操作是将列克隆到新对象并将新对象添加到新数据表。

+0

良好的信息。请记住,克隆函数将在内存中创建一个新对象,因此您可能必须将其转换回原始类型。此外,如果不需要,请务必处理旧对象。最后,GetChanges函数非常有用,因为它也会在内存中创建一个新对象,但只返回受影响的记录。 – 2014-10-07 17:22:09