I have a datagridview and I want to pass some of its value to another datagridview after selecting the values and clicking a command button. Is there any possible ways for me to do it? Thanks! God Bless. :D

Hi
you need Single selection or multiple selection?
If you want to have multiple selection of rows have you provided any checkbox in Grid view column?

hi
suppose u have two datagrid view

1. DataGridView1 (source to copy from)
2. DataGridView2 (Destination to copy)

For example assume that both the datagridview have 3 columns

u have a button to perform the copy.. write the following code at copy button

Dim dr As New System.Windows.Forms.DataGridViewRow
        For Each dr In Me.DataGridView1.SelectedRows
            Me.DataGridView2.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value, dr.Cells(2).Value)
        Next

hope it helps

Multiple probably. Thanks, after putting check boxes on my datagridview what is the next thing to do? :D

@sandee - I tried your code but it isn't working. No values were passed on my second datagridview. Thank you very much. :)

here is my complete code:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.Columns.Add("1", "1")
        Me.DataGridView1.Columns.Add("2", "1")
        Me.DataGridView1.Columns.Add("3", "3")

        Me.DataGridView2.Columns.Add("1", "1")
        Me.DataGridView2.Columns.Add("2", "1")
        Me.DataGridView2.Columns.Add("3", "3")

        Me.DataGridView1.Rows.Add("1", "2", "3")
        Me.DataGridView1.Rows.Add("4", "5", "6")
        Me.DataGridView1.Rows.Add("7", "8", "9")
        Me.DataGridView1.Rows.Add("10", "11", "12")
        Me.DataGridView1.Rows.Add("13", "14", "15")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dr As New System.Windows.Forms.DataGridViewRow
        For Each dr In Me.DataGridView1.SelectedRows
            Me.DataGridView2.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value, dr.Cells(2).Value)
        Next

       
    End Sub
End Class

here is an attached screenshot

when i multi select rows in datagridview1 and press button1 its works fine

Comments
its work!
Attachments Untitled.png 9.87 KB

Great! Thank you very much and I understood it. On the other hand yours is different than mine, the value of my first datagridview comes from a database. So I still can't get the output I want. :)

No, my second datagridview has no content at all. Sorry, I really have less knowledge regarding datagridview. :)

ok..

first u will have to add the same number of column to datagridview2(destination)

here is the modified code of my example:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.Columns.Add("1", "1")
        Me.DataGridView1.Columns.Add("2", "1")
        Me.DataGridView1.Columns.Add("3", "3")



        Dim i As Integer = 0
        For i = 0 To Me.DataGridView1.Columns.Count - 1
            Me.DataGridView2.Columns.Add(Me.DataGridView1.Columns(i).Name, Me.DataGridView1.Columns(i).HeaderText)
        Next


        Me.DataGridView1.Rows.Add("1", "2", "3")
        Me.DataGridView1.Rows.Add("4", "5", "6")
        Me.DataGridView1.Rows.Add("7", "8", "9")
        Me.DataGridView1.Rows.Add("10", "11", "12")
        Me.DataGridView1.Rows.Add("13", "14", "15")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dr As New System.Windows.Forms.DataGridViewRow
        For Each dr In Me.DataGridView1.SelectedRows
            Me.DataGridView2.Rows.Add(dr.Cells(0).Value, dr.Cells(1).Value, dr.Cells(2).Value)
        Next

        ' IsDBNull(Me.DataGridView1.Rows.Count)
    End Sub

see the change i Form_Load Event..

i have programatically added the same number of column in datagridview2(destination) as datagridview1(source)

Get all the selected rows in loop
declare new row and datatable , Add the selected row to datatable and then assign this datatable to data set and bind the dataset to GRIDVIEW

Here's the flow of my program:

On my txtExamTemplateNumber (textbox) I'll put a number and loads it. If there's a value found, the value will be displayed in the first datagridview (I'm done with that). Now my problem is, among those values displayed on the first datagridview, I will choose some values there and put them on the second datagridview. I am really sorry, I hope you are not being disturbed. Anyways, I'm so thankful to you. God Bless you! :)

u can do it with the code i provided you .. u just have to select multiple rows by pressing CTRL and selecting the entire row..

This article has been dead for over six months. Start a new discussion instead.