Home > Blockchain >  How to access DataGridView properties on an already designed Windows Form
How to access DataGridView properties on an already designed Windows Form

Time:04-27

I had already designed a windows form (Form1) with a DataGridView on it, I also have a ribbon with a button on it in which I want when I click on it, it run a code like bellow:

Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles 
Button2.Click
    Dim f As Form1
    f = New Form1
    f.Show()
'(Room for my question)
end sub

I want to be able to access my datagridview user control to add columns and rows programmatically.

How should I do that?

CodePudding user response:

You can create a Public Sub on form one which where you will put the changes that you want make on the the Datagridview then call it on the Button2_Click event of your Form2.

Example: Form 1:

Public Sub AddToDGV()
    With DataGridView1
        .Rows.Add(Form2.TextBox1.Text, Form2.TextBox2.Text, Form2.TextBox3.Text)
    End With
End Sub

Form2:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Call Form1.AddToDGV()
End Sub
  • Related