Hi guys,

I am new to vb.net i want to know how to pass a datagridview row values onto another form once the row is been double clicked.

Recommended Answers

All 4 Replies

Hi

One way to do this would be to use the SelectedRows property of the DataGridView and when it is double clicked you could pass this to your next form. For example, if I had two Forms (Form1 and Form2) and Form1 contained a DataGridView I could set it up so that when the DataGridView is double clicked and one or more rows was selected I could pass these to Form2 via a property.

So, on Form2 you could have the following property:

    Public Property SelectedRows As DataGridViewSelectedRowCollection

This would appear just below the Public Class Form2 line.

Then in Form1, for the DoubleClick event of the DataGridView you could do the following:

    Private Sub DataGridView1_DoubleClick(sender As Object, e As EventArgs) Handles DataGridView1.DoubleClick

        If DataGridView1.SelectedRows.Count > 0 Then

            Dim newForm As New Form2
            newForm.SelectedRows = DataGridView1.SelectedRows
            newForm.Show()

        End If

    End Sub

Then to read from the SelectedRows property on Form2 you could use code similar too:

    MessageBox.Show(SelectedRows(0).Cells(1).Value)

Which will return the value of the first row, second column.

HTH

commented: Perfect supportings +5

thank you soo much djjeavons. it worked just as i wanted it to..

I have two forms. i already have some values has been displayed in datagrid view in one form including name. In another form, i additionally add some more values including name field. I need to pass the second form values to the datagrid which was in first form by comparing name fields.

I have tried the following code example above to my project without any success can someone please help.
Here is a copy of my code

On the schedule_frm i declared: Public Property SelectRows As DataGridViewSelectedRowCollection
under the Public Class Schedule_Frm form class

Public Class Main_Frm

Private Sub TBL_TEAMSBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBL_TEAMSBindingNavigatorSaveItem.Click
    Me.Validate()
    Me.TBL_TEAMSBindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.FootBallDataSet)

End Sub

Private Sub Main_Frm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'FootBallDataSet.TBL_TEAMS' table. You can move, or remove it, as needed.
    Me.TBL_TEAMSTableAdapter.Fill(Me.FootBallDataSet.TBL_TEAMS)

End Sub

Private Sub TBL_TEAMSDataGridView_Click(sender As Object, e As EventArgs) Handles TBL_TEAMSDataGridView.Click

    If TBL_TEAMSDataGridView.SelectedRows.Count Then
        Dim Schedule_Form As New Schedule_Frm
        Schedule_Form.SelectRows = TBL_TEAMSDataGridView.SelectedRows
        Schedule_Form.ShowDialog()
    End If

End Sub

End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.