none
how to copy and paste selected rows from one datagridview to another form's datagridview in vb 2010?

    Question

  • hi everyone,

    I have two datagridview in separate forms calld datagridview1 and datagridview2, i want select rows in datagridview1 and then clicking a bottun in form1, it should show form2 and past selected rows of datagridview1 into datagridview2 and then i save data from datagridview2 to the databse.

    pls help me and show me an perfect example.


    • Edited by rasol Saturday, January 11, 2014 8:37 AM
    Saturday, January 11, 2014 8:26 AM

Answers

  • Kevin made the following VS2010 Solution in VB.NET, it has two projects, one for allowing a user to select rows in a DataGridView via checking a CheckBox cell then pressing CTRL+V to set the selected rows to the Windows Clipboard comma delimited. The second project accepts this data or Excel data by selecting cells, pressing CTRL+C.

    https://skydrive.live.com/redir?resid=A3D5A9A9A28080D1!194

    This thread is about how to copy selected rows/cells from one datagridview to another:
    http://social.msdn.microsoft.com/Forums/en-US/8c4c8eda-10b5-4393-84c8-418ada34ae98/copy-selected-rowscells-from-one-datagridview-to-another

    Best Regards,
    Please remember to mark the replies as answers if they help

    Monday, January 13, 2014 8:35 AM
  • Quick and dirty

    first form

    Public Class Form9
        Dim mytab As New DataTable
        Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim con As New SqlClient.SqlConnection("Data Source=yourserver;Initial Catalog=yourDB;Integrated Security=True")
            Dim da As New SqlClient.SqlDataAdapter("Select * from yourtable", con)
            da.Fill(mytab)
            DataGridView1.DataSource = mytab
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim mytab2 As DataTable = mytab.Clone
    
            For Each dgrow As DataGridViewRow In DataGridView1.SelectedRows
                Dim dr As DataRow
                dr = mytab2.NewRow
                dr.ItemArray = DirectCast(dgrow.DataBoundItem, DataRowView).Row.ItemArray
                mytab2.Rows.Add(dr)
            Next
            Dim frm As New Form10(mytab2)
            frm.Show()
        End Sub
    End Class

    'FORM 2

    Option Strict On Public Class Form10 Dim table2 As New DataTable Sub New(tab2 As DataTable) ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. table2 = tab2 End Sub Private Sub Form10_Load(sender As Object, e As EventArgs) Handles Me.Load DataGridView1.DataSource = table2 End Sub End Class



    Monday, January 13, 2014 10:05 AM

All replies

  • It seems to me that you want people in this forum make programs for you.

    First try it yourself, create your program with 2 datagridviews which are filled from a database and tell then what problems you have.

    Be aware we help people to become a programmer, or professional programmers if something is not clear for them, we are not stealing the food out of the mouth from those persons who earn a living with programming.

     



    Success
    Cor

    Saturday, January 11, 2014 9:05 AM
  • Hi cor,

    What doyou mean?

    Many people do not search the Internet properly because they are new in social.msdn or Celebrity Scripting Sites, But we can show themlinkson the internet, if you call this :The labor of others are eating,

    Also,I do not claim to programming, If I find any channels to resolvemy issue, I do not question.

    Thank's for your instruction.

    best regards.


    • Edited by rasol Saturday, January 11, 2014 10:42 AM m
    Saturday, January 11, 2014 10:37 AM
  • Rasol,

    I've seen you also try to help others. We should avoid that somebody gets the idea you can put in this forum an order to create a program. 

    Your question can be answered, but it will need a lot of code. That is not the intention of this forum.

    Be aware if I write something it is not always only for the OP (original poster) but moreover for those visiting this forum without asking a question and use it as knowledge-base.


    Success
    Cor

    Saturday, January 11, 2014 10:46 AM
  • Kevin made the following VS2010 Solution in VB.NET, it has two projects, one for allowing a user to select rows in a DataGridView via checking a CheckBox cell then pressing CTRL+V to set the selected rows to the Windows Clipboard comma delimited. The second project accepts this data or Excel data by selecting cells, pressing CTRL+C.

    https://skydrive.live.com/redir?resid=A3D5A9A9A28080D1!194

    This thread is about how to copy selected rows/cells from one datagridview to another:
    http://social.msdn.microsoft.com/Forums/en-US/8c4c8eda-10b5-4393-84c8-418ada34ae98/copy-selected-rowscells-from-one-datagridview-to-another

    Best Regards,
    Please remember to mark the replies as answers if they help

    Monday, January 13, 2014 8:35 AM
  • Quick and dirty

    first form

    Public Class Form9
        Dim mytab As New DataTable
        Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim con As New SqlClient.SqlConnection("Data Source=yourserver;Initial Catalog=yourDB;Integrated Security=True")
            Dim da As New SqlClient.SqlDataAdapter("Select * from yourtable", con)
            da.Fill(mytab)
            DataGridView1.DataSource = mytab
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim mytab2 As DataTable = mytab.Clone
    
            For Each dgrow As DataGridViewRow In DataGridView1.SelectedRows
                Dim dr As DataRow
                dr = mytab2.NewRow
                dr.ItemArray = DirectCast(dgrow.DataBoundItem, DataRowView).Row.ItemArray
                mytab2.Rows.Add(dr)
            Next
            Dim frm As New Form10(mytab2)
            frm.Show()
        End Sub
    End Class

    'FORM 2

    Option Strict On Public Class Form10 Dim table2 As New DataTable Sub New(tab2 As DataTable) ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. table2 = tab2 End Sub Private Sub Form10_Load(sender As Object, e As EventArgs) Handles Me.Load DataGridView1.DataSource = table2 End Sub End Class



    Monday, January 13, 2014 10:05 AM