错误:列***已经属于另一个dataTable

时间:2011-04-18 07:14:15

标签: asp.net vb.net

我尝试过滤一些数据并添加到另一个数据表中。运行时错误显示

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

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

2 个答案:

答案 0 :(得分:2)

您可以尝试类似

的内容
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

答案 1 :(得分:1)

您不能将dataTable中的列添加到另一个dataTable中,因为如果它将通过ref复制,并且如果您将更改一个数据表中的列,则它将在另一个数据表中更改... 您可以做的是将列克隆到新对象并将新对象添加到新数据表。