简体   繁体   中英

How to refresh after inserting data into database?

I am new to c#. After inserting data into my database, i have a form which allows me to view my data where the DataGridView would display all the data. However, the table does not display the new data that is being inserted into the database.

Have researched on datagridview.refresh() but its only graphical refresh.

Following is my code: (No idea whether I'm on the right track or not)

  private void button_refresh_Click(object sender, EventArgs e)
        {
            //DataGridView1.refresh(); 
            //DataSet1.GetChanges();
            //TableAdapter.Fill(DataSet1.table);
        }

If you are not using data binding then the only way is to remove all the rows:

DataGridView1.Rows.Clear();

and then read and add rows again as you did initially

let's imagine d is the DataSource I have

d.Add(new string[] {"a", "b", "c"});
dataGridView1.DataSource = d;

Now my DataGridView has 1 row

after a while ...

d.Add(new string[] { "1", "2", "3" });
d.Add(new string[] { "4", "5", "6" });
d.Add(new string[] { "x", "y", "z" });
dataGridView1.DataSource = null;
dataGridView1.DataSource = d;
dataGridView1.Refresh();

Now my DataGridView has 4 rows.

this.employeesTableAdapter.Fill(this.dataSet1.Employees);

Where: Employees your table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM