开发者

Error: Column *** already belongs to another dataTable

开发者 https://www.devze.com 2023-02-25 07:40 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号