Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

VB
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


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 ??
Posted
Updated 31-Oct-17 10:16am
Comments
Maciej Los 31-Oct-17 15:56pm    
What is: Selected? Seems, it's second WinForm...

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900