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).