Error: Column *** already belongs to another dataTable
I am try to filter some data and add to another datatable. While Run time the Error is shows
Column *** already belongs to another 开发者_开发问答dataTable
Here My code:
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
What is my issue? Any one give me solution..
You can try something like
Result = FullTable.Clone(); //replicate the structure
And then
If .Rows(i)(5).ToString == Mode Then // i belive you need comparison
Result.ImportRow(.Rows(i)); // Add this row
End If
You can`t add column from dataTable to another dataTable because if it will copy by ref and if you will change the column in one datatable it will change in the other as well...
What you can do is to clone the column to new object and add the new object to the new datatable.
精彩评论