将选定的行从一个datagridview复制到另一个VB.NET [英] Copy selected rows from one datagridview to another VB.NET

查看:79
本文介绍了将选定的行从一个datagridview复制到另一个VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在2个表格上有2个dgvws。第一个dgvw有一个checbox列。我想将已检查的行从dgvw 1复制到dgvw 2 ..没有任何按钮点击...我的意思是一旦用户检查一行的复选框,行就会自行复制...



我尝试了什么:



我使用以下代码:



I have 2 dgvws on 2 forms. The first dgvw has a checbox column.I want to copy the checked rows from dgvw 1 to dgvw 2..without any button click..i mean the rows will copy themselves as soon as the user checks a row's checkbox...

What I have tried:

I am using the below code :

Private Sub userdatagrid_SelectionChanged(sender As Object, e As EventArgs) Handles userdatagrid.SelectionChanged
       Dim c, t As Integer
       Selected.dg2.Columns.Clear()
       For t = 0 To userdatagrid.Columns.Count - 1
           Selected.dg2.Columns.Add(userdatagrid.Columns(t).Clone())
       Next
       For c = 0 To userdatagrid.Rows.Count - 1
           If userdatagrid.Rows(c).Cells(0).Value = True Then
               Selected.dg2.Rows.Add(userdatagrid.Rows(c).Cells(0).Value, userdatagrid.Rows(c).Cells(1).Value, userdatagrid.Rows(c).Cells(2).Value, userdatagrid.Rows(c).Cells(3).Value, userdatagrid.Rows(c).Cells(4).Value, userdatagrid.Rows(c).Cells(5).Value, userdatagrid.Rows(c).Cells(6).Value, userdatagrid.Rows(c).Cells(7).Value, userdatagrid.Rows(c).Cells(8).Value, userdatagrid.Rows(c).Cells(9).Value, userdatagrid.Rows(c).Cells(10).Value, userdatagrid.Rows(c).Cells(11).Value, userdatagrid.Rows(c).Cells(12).Value, userdatagrid.Rows(c).Cells(13).Value, userdatagrid.Rows(c).Cells(14).Value, userdatagrid.Rows(c).Cells(15).Value, userdatagrid.Rows(c).Cells(16).Value, userdatagrid.Rows(c).Cells(17).Value, userdatagrid.Rows(c).Cells(18).Value, userdatagrid.Rows(c).Cells(19).Value, userdatagrid.Rows(c).Cells(20).Value, userdatagrid.Rows(c).Cells(21).Value, userdatagrid.Rows(c).Cells(22).Value, userdatagrid.Rows(c).Cells(23).Value, userdatagrid.Rows(c).Cells(24).Value, userdatagrid.Rows(c).Cells(25).Value, userdatagrid.Rows(c).Cells(26).Value, userdatagrid.Rows(c).Cells(27).Value, userdatagrid.Rows(c).Cells(28).Value, userdatagrid.Rows(c).Cells(29).Value, userdatagrid.Rows(c).Cells(30).Value)
           End If
       Next
   End Sub

  Private Sub form2_load(sender As Object, e As EventArgs) Handles MyBase.Load
       Dim img As DataGridViewImageColumn
       img = dg2.Columns(1)
       img.ImageLayout = DataGridViewImageCellLayout.Stretch
   End Sub
  Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
       Selected.Show()
   End Sub





问题

你不能投票你自己的帖子

0

你好,我现在使用这个代码:



Private Sub userdatagrid_SelectionChanged(发送者) As Object,e As EventArgs)处理userdatagrid.SelectionChanged

Dim c,t As Integer

Selected.dg2.Columns.Clear()

对于t = 0到userdatagrid.Columns.Count - 1

Selected.dg2.Columns.Add(userdatagrid.Columns(t).Clone())

下一步

对于c = 0到userdatagrid.Rows.Count - 1

如果userdatagrid.Rows(c).Cells(0).Value = True则

Selected.dg2.Rows.Add(userdatagrid.Rows(c).Cells(0).Value,userdatagrid.Rows(c).Cells(1).Value,userdatagrid.Rows(c).Cells(2).Value ,userdatagrid.Rows(c).Cells(3)。Value,userdatagrid.Rows(c).Cells(4)。Value,userdatagrid.Rows(c).Cells(5).Value,userdatagrid.Rows(c)。细胞(6 )。Value,userdatagrid.Rows(c).Cells(7)。Value,userdatagrid.Rows(c).Cells(8)。Value,userdatagrid.Rows(c).Cells(9).Value,userdatagrid.Rows( c).Cells(10)。Value,userdatagrid.Rows(c).Cells(11)。Value,userdatagrid.Rows(c).Cells(12)。Value,userdatagrid.Rows(c).Cells(13)。 Value,userdatagrid.Rows(c).Cells(14).Value,userdatagrid.Rows(c).Cells(15)。Value,userdatagrid.Rows(c).Cells(16).Value,userdatagrid.Rows(c) .Cells(17)。Value,userdatagrid.Rows(c).Cells(18).Value,userdatagrid.Rows(c).Cells(19).Value,userdatagrid.Rows(c).Cells(20).Value, userdatagrid.Rows(c).Cells(21).Value,userdatagrid.Rows(c).Cells(22)。Value,userdatagrid.Rows(c).Cells(23).Value,userdatagrid.Rows(c).Cells (24)。Value,userdatagrid.Rows(c).Cells(25).Value,userdatagrid.Rows(c).Cells(26)。Value,userdatagrid.Rows(c).Cells(27).Value,userdatagrid。行(c).Cells(28).Value,userdatagrid.Rows(c).Cells(29)。Value,userdatagrid.Rows(c).Cells(30).Value)

结束If

下一页

结束子



Private Sub form2_load(发送者为对象,e为EventArgs)处理MyBase.Load

Dim img As DataGridViewImageColumn

img = dg2.Columns(1)

img.ImageLayout = DataGridViewImageCellLayout.Stretch

End Sub

Private Sub Button6_Click(sender As Object,e As EventArgs )处理Button6.Click

Selected.Show()

结束子

此代码的问题是,如果我只检查一行/复选框并单击按钮6,表单2上的dgvw显示为br rmpty。我需要选择/检查两行,然后只显示其中一行。第二个问题是,假设用户只检查一个复选框,并单击按钮,表单2上的dgv为空(我已经说过)但是如果用户关闭表单2并使用button6重新打开它,它会给出一个INDEX OUT OF RANGE例外形式2的IMG = DG2.COLUMNS(1)....如何解决这些问题?



Question
You cannot vote on your own post
0
hello, i am using this code now :

Private Sub userdatagrid_SelectionChanged(sender As Object, e As EventArgs) Handles userdatagrid.SelectionChanged
Dim c, t As Integer
Selected.dg2.Columns.Clear()
For t = 0 To userdatagrid.Columns.Count - 1
Selected.dg2.Columns.Add(userdatagrid.Columns(t).Clone())
Next
For c = 0 To userdatagrid.Rows.Count - 1
If userdatagrid.Rows(c).Cells(0).Value = True Then
Selected.dg2.Rows.Add(userdatagrid.Rows(c).Cells(0).Value, userdatagrid.Rows(c).Cells(1).Value, userdatagrid.Rows(c).Cells(2).Value, userdatagrid.Rows(c).Cells(3).Value, userdatagrid.Rows(c).Cells(4).Value, userdatagrid.Rows(c).Cells(5).Value, userdatagrid.Rows(c).Cells(6).Value, userdatagrid.Rows(c).Cells(7).Value, userdatagrid.Rows(c).Cells(8).Value, userdatagrid.Rows(c).Cells(9).Value, userdatagrid.Rows(c).Cells(10).Value, userdatagrid.Rows(c).Cells(11).Value, userdatagrid.Rows(c).Cells(12).Value, userdatagrid.Rows(c).Cells(13).Value, userdatagrid.Rows(c).Cells(14).Value, userdatagrid.Rows(c).Cells(15).Value, userdatagrid.Rows(c).Cells(16).Value, userdatagrid.Rows(c).Cells(17).Value, userdatagrid.Rows(c).Cells(18).Value, userdatagrid.Rows(c).Cells(19).Value, userdatagrid.Rows(c).Cells(20).Value, userdatagrid.Rows(c).Cells(21).Value, userdatagrid.Rows(c).Cells(22).Value, userdatagrid.Rows(c).Cells(23).Value, userdatagrid.Rows(c).Cells(24).Value, userdatagrid.Rows(c).Cells(25).Value, userdatagrid.Rows(c).Cells(26).Value, userdatagrid.Rows(c).Cells(27).Value, userdatagrid.Rows(c).Cells(28).Value, userdatagrid.Rows(c).Cells(29).Value, userdatagrid.Rows(c).Cells(30).Value)
End If
Next
End Sub

Private Sub form2_load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim img As DataGridViewImageColumn
img = dg2.Columns(1)
img.ImageLayout = DataGridViewImageCellLayout.Stretch
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Selected.Show()
End Sub
The problem with this code is, if i check only one row/checkbox and click on button 6, the dgvw on form 2 appears to br rmpty. I need to select/check two rows and then only one of the rows appear. The second problem is, suppose a user checks only one checkbox, and clicks on the button , the dgv on form 2 is empty(i said that already) but if the user closes form 2 and reopens it with button6, it gives an INDEX OUT OF RANGE exception in form 2's IMG=DG2.COLUMNS(1) ....how to fix these problems ??

推荐答案

检查过去的问题和答案:

如何将DataGridView行复制到另一个CheckBoxColumns中检入VB.NET [ ^ ]

将选定的行复制到另一个datagridview [ ^ ]

在VB.NET中将数据从一个DataGridView复制到另一个DataGridView上 [ ^ ]
Check past questions and answers:
How To DataGridView Rows Copy To Another On CheckBoxColumns Checked In VB.NET[^]
copy selected rows to another datagridview[^]
Copy rows from One DataGridView To Another On DataGridView In VB.NET[^]


这篇关于将选定的行从一个datagridview复制到另一个VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆