Answer the question
In order to leave comments, you need to log in
Windows forms, just learning?
there is a save button and a view button, the second shows the database, and the first saves to it, but to see what has been saved, you need to press view. what is the actual question, how to make it so that what is saved is automatically shown in the datagridview, and you don’t have to press the view each time
, I provide the code of two buttons, but as I understand it, one needs to be added and the other removed altogether))
private void button1_Click(object sender, EventArgs e)
{
conpoezdb.Open();
SqlDataAdapter SDA = new SqlDataAdapter("INSERT INTO train(number,place, count, data, time)VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')", conpoezdb);
SDA.SelectCommand.ExecuteNonQuery();
conpoezdb.Close();
MessageBox.Show("saved");
}
private void button4_Click(object sender, EventArgs e)
{
conpoezdb.Open();
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM train", conpoezdb);
DataTable data = new DataTable();
SDA.Fill(data);
dataGridView1.DataSource = data;
conpoezdb.Close();
}
Answer the question
In order to leave comments, you need to log in
In this case, the simplest is like this:
private void button1_Click(object sender, EventArgs e)
{
conpoezdb.Open();
SqlDataAdapter SDA = new SqlDataAdapter("INSERT INTO train(number,place, count, data, time)VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')", conpoezdb);
SDA.SelectCommand.ExecuteNonQuery();
conpoezdb.Close();
MessageBox.Show("saved");
updateView();
}
private void button4_Click(object sender, EventArgs e)
{
updateView();
}
private void updateView()
{
conpoezdb.Open();
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM train", conpoezdb);
DataTable data = new DataTable();
SDA.Fill(data);
dataGridView1.DataSource = data;
conpoezdb.Close();
}
private void button1_Click(object sender, EventArgs e)
{
conpoezdb.Open();
//пишем в бд
SqlDataAdapter SDA = new SqlDataAdapter("INSERT INTO train(number,place, count, data, time)VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "')", conpoezdb);
SDA.SelectCommand.ExecuteNonQuery();
//запрашиваем по новой
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM train", conpoezdb);
conpoezdb.Close();
DataTable data = new DataTable();
SDA.Fill(data);
dataGridView1.DataSource = data;
MessageBox.Show("saved");
}
1. SqlDataAdapter SDA = new SqlDataAdapter("INSERT INTO train(number,place, count, data, time)VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "','" + textBox3. Text + "','" + textBox4.Text + "','" + textBox5.Text + "')", conpoezdb);
Not safe
Must use . For example
https://msdn.microsoft.com/en-us/library/bbw6zyha(...
2. try catch no, add to avoid trouble in the future.
3. The above question has been answered.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question