I'm trying to take some basic form data (item and quantity), from a form where this sort of information is filled out by a user, and after the user clicks "ADD", the entered info is moved to a DataGridView on a different page.

The following code:

Me.DataGridView1.Rows.Add(...)

...does not work for this case since the DataGridView is in another form; how do I get the two pieces of data to carry over to a different form?

Also, if anybody could help me with another thing, I also have a 'drop-down' category on the "ADD" form, for the type of item the user is adding. How would I code it so that, for example, a user chooses the category "Food", it would fall under the "Food" tab? (NOTE: I'm using TabControl)

Recommended Answers

All 4 Replies

Me.DataGridView1.Rows.Add(...)

To access an object of a Form from another, you must call the Form Name first i.e. <FormName>.<ObjectName>.<PropertyName>=<Value>. So your syntax should be<YourFormName>.DataGridView1.Rows.Add(...).

Thank you for replying.
Unfortunately though, I've tried that.
The "ADD" form I've named 'additem', and the form which the DataGridView is on, I've named 'invmain'.
Here is the code that I used on the 'additem' form when the user clicks the "ADD" button:
invmain.DataGridView1.Rows.Add(itembox.Text, quantitybox.Text)
When I run it, and attempt to add an item w/ quantity, it says, "Invalid Operation Exception was handled".

Also, another thing I should note, is that apart from having the TabControl, I have four tabs of different categories, and each tab has a DataGridView.

Well with tabcontrols you also have to select the pages of the control you want to work with. Also you need to add columns to a datagridview before you can add the rows. So here is a small application to show you how to it. I move text from form1 to all other pages in form1 and form2 and create a populate a datagridview on form2 form form1.

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = "Hello"
        Form2.Show()
    End Sub
    Private Sub TabPage1_Click(sender As Object, e As EventArgs) Handles TabPage1.Click
    End Sub
    Private Sub Exit_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub
    Private Sub Transfer_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'To work with the tabcontrol you need to select it and also
        'the page of the control
        Dim selectedTab As TabPage = TabControl1.SelectedTab
        Dim selectedIndex As Integer = TabControl1.SelectedIndex
        TabControl1.SelectedTab = selectedTab

        Dim str As String
        str = Me.TextBox1.Text 'Textbox in form1,tabcontrol1,page1
        'Page indexes are 0 based
        If TextBox1.Text.Length > 0 Then
            'here we select Textbox2 in form1,tabcontrol1,page2
            TabControl1.SelectedTab = TabControl1.TabPages(1)
            TextBox2.Text = str & "Page2" 'transfer text from page 1 to page2
            'next select form2,tabcontrol1 in form2, and page1 of thetabcontrol
            Form2.TabControl1.SelectedTab = Form2.TabControl1.TabPages(0)
            'transfer text to textbox in tabcontrol on form 2,page 1 textbox1
            Form2.TextBox1.Text = str & "Form2 Page1"
            Form2.TabControl1.SelectedTab = Form2.TabControl1.TabPages(1)
            'herewedothesameon tabpage2 on form2
            Form2.TextBox2.Text = str & "Form2 Page2"
            'now we deal with the datagridview1 on form2,in tabcontrol page 2
            'You need to add the columns first
            'Datagrid in form2, tabcontrol1,page2
            Form2.DataGridView1.ColumnCount = 3
            Form2.DataGridView1.Columns(0).Name = "Hello"
            Form2.DataGridView1.Columns(1).Name = "I am"
            Form2.DataGridView1.Columns(2).Name = "Minimalist"
            'Add the rows to the datagridview1 in form2, in tabcontrol page 2
            Dim row As String() = New String() {"Row1", "Row1 Column 2", "Row1 Column 3"}
            Form2.DataGridView1.Rows.Add(row)
            row = New String() {"Row2", "Row2 Column 2", "Row2 Column 3"}
            Form2.DataGridView1.Rows.Add(row)
            row = New String() {"Row3", "Row3 Column 2", "Row3 Column 3"}
            Form2.DataGridView1.Rows.Add(row)
            row = New String() {"Row4", "Row4 Column 2", "Row4 Column 3"}
            Form2.DataGridView1.Rows.Add(row)
        End If
    End Sub
End Class

Tabcontrol.jpg

Thank you for all the replies.

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.