N
N
Nastya06962016-05-14 20:49:24
C++ / C#
Nastya0696, 2016-05-14 20:49:24

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

2 answer(s)
D
Dmitry Eremin, 2016-05-14
@Nastya0696

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();
        }

In general, we move the datagridview update logic into a separate method and pull it by clicking on the view and / or clicking on save
UPD: well, this is provided that you need the view button
UPD1: like this, if without the view button
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");
        }

#
#algooptimize #bottize, 2016-05-14
@user004

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 question

Ask a Question

731 491 924 answers to any question