vb.netdatagridviewowner

Update datagridview in different forms


I can't insert a string before a datagridview How can I pass Form1..Form2..Form3 variable to datagridview?

I have a datagridview with the same name in multiple forms I need to get the values ​​from them by changing the name "FORMNAMEXXXX".DataGridView1.CurrentRow.Cells(0).Value

Dim ownerType = Owner.GetType()
Dim formowner = ownerType.Name.ToString
Dim id As Integer = formowner.DataGridView1.CurrentRow.Cells(0).Value.ToString

but I have this error: 'DataGridView1' is not a member of 'String'


Solution

  • I have 3 forms, in form1 and form2 there is a datagridview. with the form3 I have to get the value of the cell of the form that called the form3. this is my requirement. I don't want to create one Public property. I was able to get the name of the form which called form3 but if I use XXXXNAME.DataGridView1.CurrentRow.Cells(0).Value.ToString I have this errore 'DataGridView1' is not a member of 'String'

    So in Form1 and Form2, you need to pass in the "owner" when you display Form3:

    ' ... in Form1 / Form2 ...
    Form3 f3 = new Form3()
    f3.ShowDialog(Me) ' <-- pass in Form1/Form2 via "Me"
    ' ... or possibly ...
    f3.Show(Me)
    

    Now over in Form3, you can use the .Owner property to get a reference to either Form1 or Form2. Afterwards, you can SEARCH for that DataGridView "by name":

    ' ... in Form3 ...
    Dim frm As Form = Me.Owner
    Dim ctl As Control = frm.Controls.Find("DataGridView1", True).FirstOrDefault()
    If Not IsNothing(ctl) AndAlso TypeOf ctl Is DataGridView Then
        Dim dgv As DataGridView = DirectCast(ctl, DataGridView)
        ' ... do something with "dgv" ...
        Debug.Print(dgv.CurrentRow.Cells(0).Value.ToString)
    End If