Jeffrey Oliveras
Jeffrey Oliveras

Reputation: 75

VB.NET: How to copy selected cell data to the first selected cell in other form?

My question may be confusing but here is the step by step process of the output I want to accomplish.

first when I click a cell in the fist form, another form pop's up. see images..

Image 1 Image 2

then if I click the cell data of the second form it should copy the selected row data of Code and Short Name to the first datagridview cell in form 1

Here is my code so far:

Private Sub SearchJournalGrid_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles SearchJournalGrid.CellContentDoubleClick
      With GeneralJournal
            .GridJournal.Rows(.GridJournal.SelectedRows(0).Index).Cells("Code").Value = SearchJournalGrid.Rows(SearchJournalGrid.SelectedRows(0).Index).Cells("CODE").Value.ToString
            .GridJournal.Rows(.GridJournal.SelectedRows(0).Index).Cells("Account Name").Value = SearchJournalGrid.Rows(SearchJournalGrid.SelectedRows(0).Index).Cells("Short Name").Value.ToString
      End With
End Sub

Upvotes: 0

Views: 311

Answers (1)

pouyan khodaverdi
pouyan khodaverdi

Reputation: 26

Let me know if I got it right!

Start a new project with only two forms in it, Form1 & Form2, add a DataGridView in each with their original name, which will DataGridView1 by default. I have created 3 columns for each of them. The code which has placed on the only Form1 is in the following:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For i As Integer = 0 To 5
        DataGridView1.Rows.Add(New String() {i + 1, "ITEM " & i + 1, "Description " & i + 1})
    Next
End Sub

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    Dim ROW_ID As Integer = DataGridView1.CurrentCell.RowIndex

    Dim DGV_Row As DataGridViewRow = DataGridView1.Rows(ROW_ID).Clone

    For J As Integer = 0 To DGV_Row.Cells.Count - 1
        DGV_Row.Cells(J).Value = DataGridView1.Rows(ROW_ID).Cells(J).Value
    Next

    With Form2
        .DataGridView1.Rows.Clear()
        .DataGridView1.Rows.Add(DGV_Row)
    End With
    Form2.Show()
End Sub

Upvotes: 1

Related Questions