【问题标题】:ImportRow from DataTable to another and still destination DataTable is Empty VB.NET从 DataTable 到另一个且仍然是目标 DataTable 的 ImportRow 是空的 VB.NET
【发布时间】:2012-04-07 20:56:55
【问题描述】:

您好,我正在尝试将一行从 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 调暗为新数据表 将 dsFrom 调暗为新数据表

        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

谢谢

【问题讨论】:

    标签: .net vb.net datatable


    【解决方案1】:

    我创建了一个带有两个 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
    

    【讨论】:

    • 谢谢,我错过了 newTable.Columns.Add("sShoes_ID", GetType(Integer))
    猜你喜欢
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多