Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 从一个datagridview到另一个datagridview的数据_Vb.net_Datagridview - Fatal编程技术网

Vb.net 从一个datagridview到另一个datagridview的数据

Vb.net 从一个datagridview到另一个datagridview的数据,vb.net,datagridview,Vb.net,Datagridview,我已经走得很远了,但这次我已经在网上搜索了好几天,尝试了好几种方法,但我似乎无法让它发挥作用。你们有什么建议吗 Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkopieren.Click For i As Integer = 0 To DGV1.Rows.Count - 1 If CBool(DGV1.Rows(i).Cells(4)

我已经走得很远了,但这次我已经在网上搜索了好几天,尝试了好几种方法,但我似乎无法让它发挥作用。你们有什么建议吗

Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkopieren.Click
  For i As Integer = 0 To DGV1.Rows.Count - 1
    If CBool(DGV1.Rows(i).Cells(4).Value) Then
        'If DGV1.Rows(i).Cells(4).Value = True Then
        DGV2.Rows.Add(DGV1.Rows(i).Cells(0).Value, _
        DGV1.Rows(i).Cells(1).Value, _
        DGV1.Rows(i).Cells(2).Value, _
        DGV1.Rows(i).Cells(3).Value)
        '2 = voer, 3 =voerid, 4= drogestof, 6 = (voer)keukenlocatie
    End If
  Next
End Sub
我得到错误“无效操作异常未处理”,显然,当我想将所选数据从一个datagridview传输到另一个绑定的datagridview时,它不喜欢这样。有什么解决办法吗?就像我说的,我试着自己修复它,用谷歌搜索,但没有结果

更新 找到以下代码。这看起来很有逻辑,看起来它可以做我想做的事情。我想我必须把它放在构建datatable的函数中,但是它现在如何获取什么数据以及将数据放在哪里呢

    For Each row As DataGridViewRow In rows
        dtRantsoen.ImportRow(DirectCast(row.DataBoundItem, DataRowView).Row)
按DataGridViewSelectedRowCollection=DGV2.SelectedRows将行变暗 下一个 '在DataGridView上分配我们的数据源 DGV2.DataSource=dtRantsoen

因此,我认为这是在表单加载事件中的DGV2.Datasource部分之前:

    Dim adapter As New OleDbDataAdapter(sql, connection)
    'Gets the records from the table and fills our adapter with those.
    Dim dtRantsoen As New DataTable("tbl_rantsoen")
    adapter.Fill(dtRantsoen)

    'Assigns our DataSource on the DataGridView
    DGV2.DataSource = dtRantsoen

不要假设错误的原因。捕获异常并查看实际的消息是什么以及导致异常的操作。一般来说,我怀疑您是否应该直接向
DataGridView
添加行,而是应该建立某种类型的集合(即使是
DataTable
也可以),然后将
DataGridView
绑定到该集合。它的名字甚至暗示了这一点,它是数据的“视图”,而不是数据的实际持久性。在这里,我需要一种简单的方法,用在另一个datagrid中选择的行填充第一个datagridview中新行的几个现有单元格。所有单元格都使用sql查询连接到数据库。概念应保持不变。用新数据填充
DataGridView
绑定到的数据源,然后重新绑定它。您好,是的,我也这么想。但问题是,它已经填充的数据也需要保留,因此我只需要代码将其他datagridview中的特定数据添加到datatable中的正确位置。我以前使用如上所示的代码填充空datagridview,但这对我来说是新的。我还尝试在datagridview中使用combobox并根据combobox选择填充其他单元格,但这不起作用,因为我必须使用sql查询中任何数据库字段中尚未输入的数据。