Eng-Mohammed AbdAllah Eng-Mohammed AbdAllah - 1 year ago 80
Vb.net Question

How can I pass value from DataGridView to another DataGridView on another form?

How can I pass values from a DataGridView to another DataGridView on another form? Please I need this answer deadly.

I have a form with a DataGridView, I am using this as invoice so at the event of end_edit for the field of item name I make a search by the first letters of this item name and put the result in another DataGridView on another form. I need to when I choose the item that I need from the form number 2(search form) to move to the same cell in the form number 1(invoice form) ?????

Thank you

Answer Source

You have to be more specific about your Question

given this scenario

form1 with a datagridview and form2 with a datagridview

you open form2 from form1 you would go to form2 and put sub new in it with reference to the main form like

Private f1 As Form1

Public Sub New(ByRef f As Form)

    Me.f1 = f
End Sub

on Form1 show your Form

    Dim f2 As New Form2(Me)

then on form 2 you have access to form1

assuming both tables have the same Datasource` bound

you can go on the ClickCell event from your Datagridview on Form2

and put in something like this to be at the same position on both DGV's

Private Sub xDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles xDataGridView.CellClick
    f1.xBindingSource.Position = Me.yBindingSource.Position
End Sub

this however will only work if you don't allow sorting of the columns otherwise it will be in defferent rows.

if you want to pass a value from dgv on form2 to dgv in form1

then you can do something like this

Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button2.Click
    f1.xDataGridView.CurrentRow.Cells("MyCell").Value = Me.xDataGridView.CurrentRow.Cells("MyCell").Value
End Sub