从DataTable导入到另一个仍然是目标的DataTable是空的VB.NET

时间:2012-04-07 20:56:55

标签: .net vb.net datatable

您好我正在尝试将一行从DataTable复制到另一行,我几乎到处都看,我找不到发生这种情况的原因,我正在使用ImportRow,而New DataTable仍然是空的。

这是我发现的类似答案之一,它仍然无效!:

   Dim newTable As New DataTable
        Dim dsFrom As New DataTable

        For Each DBShoes In list
            Dim iShoeID As Integer
            iShoeID = DBShoes.sShoes_ID
            dsFrom = DBShoes.GetFullShoeDetails(iShoeID)
            For Each dr As DataRow In dsFrom.Rows
                newTable.Rows.Add(dr.ItemArray)
            Next
        Next
        GridView1.DataSource = newTable
        GridView1.DataBind()

错误:输入数组比此表中的列数长。

这是不会崩溃的导入,但DataTable中没有添加任何内容:     昏暗的newTable作为新的DataTable             Dim dsFrom As New DataTable

        For Each DBShoes In list
            Dim iShoeID As Integer
            iShoeID = DBShoes.sShoes_ID
            dsFrom = DBShoes.GetFullShoeDetails(iShoeID)
            For Each dr As DataRow In dsFrom.Rows
                newTable.ImportRow(dr)
            Next
        Next
        GridView1.DataSource = newTable
        GridView1.DataBind()

    Else

由于

1 个答案:

答案 0 :(得分:4)

我创建了一个带有两个DataGridView控件的表单。 以下是表格代码:

Public Class Form1

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ' create table with single column
    Dim dt1 As New DataTable
    dt1.Columns.Add("Number", GetType(Integer))

    ' create another table with single column
    Dim dt2 As New DataTable
    dt2.Columns.Add("Number", GetType(Integer))

    ' fill first table with single row
    Dim r As DataRow = dt1.NewRow()
    r.Item(0) = 1
    dt1.Rows.Add(r)

    ' import all rows of first table into second table
    For Each row In dt1.Rows
        dt2.ImportRow(row)
    Next

    ' show tables
    DataGridView1.DataSource = dt1
    DataGridView2.DataSource = dt2
End Sub
End Class