-2

I have Main Form which connected with two other forms by buttons(Content Adding Form and Form with DataGridView). Form with DataGridView must be without button (which go over to Content Adding Form).

Content Adding Form:

private void button1_Click(object sender, EventArgs e)
{
    FormDataGridView main = this.Owner as FormDataGridView;
        if (main != null)
        {    
            DataRow nRow = main.databaseDataSet.Tables[0].NewRow();
            int rc = main.dataGridView1.RowCount + 1;
            nRow[0] = rc;
            nRow[1] = textBox1.Text;
            nRow[2] = textBox2.Text;
            nRow[3] = textBox3.Text;
            nRow[4] = textBox4.Text;
            main.databaseDataSet.Tables[0].Rows.
            Add(nRow);
            main.studentsTableAdapter.Update(main.databaseDataSet.students);
            main.databaseDataSet.Tables[0].AcceptChanges();
            main.dataGridView1.Refresh();
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }
    }

Main Form:

private void button2_Click(object sender, EventArgs e)
    {
        ContentAdding add = new ContentAdding();
        add.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        FormDataGridView view = new FormDataGridView();
        view.Show();
    }

https://www.linkpicture.com/q/photo_4.png

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • You should be passing the data you need into the constructor and or set a property in `ContentAdding` with your data. FWIW, I would create a new class to hold your data and add this to a collection of some sorts and finally you could set this as your `DataSource` to the grid. – Trevor Apr 28 '22 at 17:56
  • Note that you're trying to get the Form's Owner (`this.Owner`), but you never set the Owner in code (`add.Show();` should be `add.Show(this);`) – Jimi Apr 28 '22 at 18:11
  • In the `ContentAdding` form, the first line of code in the method is… `FormDataGridView main = this.Owner as FormDataGridView;` … ? … This implies that the `ContentAdding` form was created and shown by the `FormDataGridView` … ? … where is the `FormDataGridView` code and where is it creating and showing the `ContentAdding` form? – JohnG Apr 29 '22 at 01:03
  • In the current code, it looks like `MainForm` is creating and showing the `ContentAdding` form and in that case `main` will always be `null` because it is not a `FormDataGridView`… it is a `MainForm`… ? … can you clarify any of this? It appears you are making this far more complex than it has to be. – JohnG Apr 29 '22 at 01:04
  • Does this answer your question? [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – Filburt Apr 29 '22 at 06:17

1 Answers1

0

It should be sufficient to pass from main form to others your main.databaseDataSet.