1

In Form1, I have one dataGridView and in Form2 one button and textbox from where I insert data.

The problem is how can I add the data from the textbox in Form2 to the dataGridView in Form1?

I know there are many methods, but I'm looking for one that is easy to understand. This is what I've tried in Form2

public partial class Form2 : Form
{
    SqlConnection con;
    string queryString;
    SqlCommand com;
    void afis()
    {
        string cons = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Depou.mdf;Integrated Security=True;User Instance=True";
        con = new SqlConnection(cons);
        queryString = "select * FROM Trenuri";
        com = new SqlCommand(queryString, con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(com);
        da.Fill(dt);
        dataGridView1.DataSource = dt;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        con.Open();
        queryString = "insert into Trenuri(ID) values(" + textBox1.Text + ")";
        com = new SqlCommand(queryString, con);
        com.ExecuteNonQuery();
        con.Close();
        afis();
        this.Close();
    }

}

(there is the "dataGridView does not exist in the current context" error, the table being in Form1).

Amr Eladawy
  • 4,193
  • 7
  • 34
  • 52
Rares3242
  • 11
  • 2
  • This is usually solved raising an event in Form2. The Form1 instance, before showing the Form2 subscribes the event and will be notified when the relevant action happens in Form2 (ie the button_click and the relative insertion of new data). At this point the first form can do whatever it likes to update its grid. – Steve May 21 '17 at 08:41
  • And how can I subscribe the event?Im a beginner so i dont know many things. – Rares3242 May 21 '17 at 08:50
  • http://stackoverflow.com/questions/2192561/c-sharp-event-handling-between-two-forms or http://stackoverflow.com/questions/1069474/how-to-raise-an-event-between-two-different-forms-of-the-same-program – Steve May 21 '17 at 08:57
  • And also this from myself http://stackoverflow.com/questions/42871909/refreshing-combobox-from-another-form/42872091#42872091 – Steve May 21 '17 at 09:01
  • 1
    You're doing things the wrong way around. `Form1` should be displaying `Form2` as a modal dialogue, i.e. calling its `ShowDialog` method. That `ShowDialog` should return `OK` if data is provided or `Cancel` otherwise. If it's `OK`, `Form1` should get the new data from properties of `Form2`, where `Form2` has made the data available. `Form1` should then add the data to the grid first, then save the changes back to the database. `Form2` should not even know that `Form1` or the database exist. – jmcilhinney May 21 '17 at 09:32

1 Answers1

-1

There is a lot of ways to do it but I recommend you using Mediator Design Pattern. Read here more about this http://www.dofactory.com/net/mediator-design-pattern it`s very interesting thing.

Rafał Developer
  • 2,135
  • 9
  • 40
  • 72
  • This is not an appropriate answer. An answer should be self-contained and not be simply a link to another page that may not be there in the future. Such links should be provided in comments, not answers. – jmcilhinney May 21 '17 at 09:29
  • @jmcilhinney ok. next time I will put this as a comment. -1 point :( hehe – Rafał Developer May 21 '17 at 09:31