I'm making a fairly simple inventory program, and I'm stuck as to how I can add data to the original main form.

On the main form, I have a button to add an item and it's details (additem). Once the user clicks the "Add" button, it should put the entered info into one of four DGV's on the main form. This works fine, however, every time a new item is added, it is added in a new instance of the main form, and the previous instance (with the previous item entry), gets thrown out.

Here is the code for the "Add Item" button on the main form:

additem form1 = new additem();
        this.Hide();
        form1.ShowDialog();
        this.Visible = true;
        this.Refresh();

And here is the code for the additem form when the user clicks "Add":

        invmain invmainobject = new invmain();

        //Work done here...

        this.DialogResult = DialogResult.OK;
        invmainobject.Show();

How do I get the information to appear on the original instance and update the form so the new entries appear in the DGV's?

That design sounds dicey to me. My preference would be for the main form to manage itself completely, where temporary dialog forms are opened as needed and pass back information objects through properties or events for the main form to own and manage beyond the lifetime of the dialog.

For example:

// Main form
List<InventoryItem> _items;

void addItem_Click(object sender, EventArgs e)
{
    using (var dlg = new AddItemDialog())
    {
        if (dlg.ShowDialog == DialogResult.OK)
        {
            _items.Add(dlg.Item);
            RefreshInventory();
        }
    }
}

// AddItemDialog
public InventoryItem Item { get; private set; }

void buttonOK_Click(object sender, EventArgs e)
{
    Item = new InventoryItem(...);

    // Populate the item with form element data

    DialogResult = DialogResult.OK;
}
commented: Well explained with good example +11

Could you please show me what you mean.
Here is my code for both the main inventory form (invmain) and add item form (additem):

invmain:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class invmain : Form
    {
        public invmain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cover form1 = new cover();
            form1.Show();
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            additem form1 = new additem();
            form1.ShowDialog();
            this.Visible = true;
            this.Refresh();
        }

        private void deletebutton_Click(object sender, EventArgs e)
        {
            DialogResult deleteitem = MessageBox.Show("Delete selected item?\t\t\t", "Delete Item", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (deleteitem == DialogResult.Yes)
            {
                foreach (DataGridViewRow item in this.datagridview1.Rows)
                {
                    datagridview1.Rows.RemoveAt(item.Index);
                }
                foreach (DataGridViewRow item in this.datagridview2.Rows)
                {
                    datagridview2.Rows.RemoveAt(item.Index);
                }
                foreach (DataGridViewRow item in this.datagridview3.Rows)
                {
                    datagridview3.Rows.RemoveAt(item.Index);
                }
                foreach (DataGridViewRow item in this.datagridview4.Rows)
                {
                    datagridview4.Rows.RemoveAt(item.Index);
                }

                MessageBox.Show("Item deleted.\t\t\t", "Delete Item", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            this.Refresh();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Update();
            this.Refresh();
        }
    }
    }

additem:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class additem : Form
    {
        public additem()
        {
            InitializeComponent();
            datetimelabel.Text = DateTime.Now.ToString("MM/dd/yyyy");
            this.quantitybox.KeyPress += new KeyPressEventHandler(quantitybox_KeyPress);

        }
        private void additem_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            invmain form1 = new invmain();
            form1.Show();
            this.Hide();
        }

        private void quantitybox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == '\b'); //OR: e.KeyChar==8
        }

        public void add_Click(object sender, EventArgs e)
        {           


            //SWITCH STATEMENT FOR DIRECTING USER-ENTERED INVENTORY DATA  
            //TO THE APPROPRIATE TABCONTROL TAB AND DGV<1:4>

            //OBJECT REFERENCE TO DGV
            invmain invmainobject = new invmain();

            switch(combobox1.SelectedIndex)
            {
                case 0: //ELECTRICAL
                    invmainobject.datagridview1.Rows.Add(itembox.Text, quantitybox.Text);
                    break;
                case 1: //MECHANICAL
                    invmainobject.datagridview2.Rows.Add(itembox.Text, quantitybox.Text);
                    break;
                case 2: //CABLES
                    invmainobject.datagridview3.Rows.Add(itembox.Text, quantitybox.Text);
                    break;
                case 3: //MISC.
                    invmainobject.datagridview4.Rows.Add(itembox.Text, quantitybox.Text);
                    break;
                default:
                    MessageBox.Show("Please select a category.\t\t", "Required Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    combobox1.Focus();
                    return;
            }

            if (string.IsNullOrWhiteSpace(this.itembox.Text))
            {
                MessageBox.Show("The 'Item' field is required.\t\t\t", "Required Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                itembox.Focus();
                return;
            }

            if (string.IsNullOrWhiteSpace(this.quantitybox.Text))
            {
                MessageBox.Show("The 'Quantity' field is required.\t\t\t", "Required Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                quantitybox.Focus();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Dispose();
            invmainobject.Show();

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
                uploadpic.Image = Bitmap.FromFile(open.FileName);
        }
    }
}

Btw, what @deceptikon means is that there shouldn't be any references to "invmain" in "additem". Also, I recommend that you use a more obvious naming strategy. If you do something like the following, it will make your code easier to follow, and also make it easier to find what you are looking for in "Solution Explorer":

Forms:
-FrmAddItem
-FrmCover
-FrmMain

Classes:
-ClsInventoryItem

If you want to use a modal child form (additem), then use "ShowDialog" as @deceptikon showed above. If you want to use a non-modal child form (additem), then use "Show". The tutorial I referenced above uses events and is for use if you use "Show".

Note: There is a slight mistake in the code @deceptikon posted above: if (dlg.ShowDialog == DialogResult.OK) should be if (dlg.ShowDialog() == DialogResult.OK)--the parentheses are missing after "ShowDialog".

What is the difference between "ShowDialog" and "Show"? One of the most noticeable differences (to the user), is that "ShowDialog" won't allow you to return to the main form until you "close" it, whereas using "Show" allows you to keep the child form open and still be able to access other forms in your program.

ShowDialog
When a form is displayed as a modal dialog box, clicking the Close button (the button with an X at the upper-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. Unlike non-modal forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.

I think the documentation for "ShowDialog" may be incorrect. In my testing, the "FormClosing" event occurs when the "X" is clicked when a form is opened using either "ShowDialog" or "Show". However, "Dispose" only occurs for "Show"--unless you use a "using" statement as @deceptikon did above or dispose of the form in some other manner. In his code above, after execution has left the "using" statement block, "Dispose" will occur.

If you do the following:

var dlg = new FrmAddItem();
dlg.ShowDialog();

Then "Dispose" will not occur when clicking the "X".

using Statement (C# Reference)
...The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object...

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.