我尝试过滤一些数据并添加到另一个数据表中。运行时,错误显示为
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
我的问题是什么?有人给我解决办法..。
发布于 2011-04-18 07:20:41
你可以尝试这样的方法
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
发布于 2011-04-18 07:22:59
您不能将列从dataTable添加到另一个dataTable,因为如果它将按引用复制,如果要在一个数据表中更改该列,则另一个数据表中的列也会更改.
您可以做的是将列克隆到新对象并将新对象添加到新的datatable中。
https://stackoverflow.com/questions/5699657
复制相似问题