1

嗨,我正在尝试将一行从 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 中没有添加任何内容的导入: 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.ImportRow(dr)
            Next
        Next
        GridView1.DataSource = newTable
        GridView1.DataBind()

    Else

谢谢

4

1 回答 1

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
于 2012-04-07T21:11:22.610 回答