Update datagridview win forms after updating database from child form

how to update datagridview after making changes to the database from another form, after closing the child form, I tried to update the datagridview using the click event, but it does not work, do I need to use a dataset?

//create an OleDbDataAdapter to execute the query dAdapter = new OleDbDataAdapter(gQuery, connString); //create a command builder cBuilder = new OleDbCommandBuilder(dAdapter); //create a DataTable to hold the query results dTable = new DataTable(); //fill the DataTable dAdapter.Fill(dTable); //BindingSource to sync DataTable and DataGridView bSource = new BindingSource(); //set the BindingSource DataSource bSource.DataSource = dTable; //set the DataGridView DataSource dataGridView1.DataSource = bSource; private void button_Refresh_Click(object sender, EventArgs e) { dataGridView1.DataSource = bSource; dataGridView1.Refresh(); } 

Please help me in advance

+4
source share
4 answers

Add

 dataGridView1.Update(); 

This will solve your problem.

+8
source

When you link your database to the "DataSource" in the DataGridView properties, the IDE automatically adds the BindingSource and TableAdapter to your form.

If the database is updated and you want to update the DataGridView, call this:

 this.<table name>TableAdapter.Fill(this.<DB name>DataSet.<table name>); 

Where <table name> is the name of your table (for example, users), and <DB name> is the name of your database (for example, MyDB).

 this.UsersTableAdapter.Fill(this.MyDBDataSet.Users); 
+3
source

You tried

 dataGridView1.DataSource = dTable; 
0
source
  bSource.DataSource = dTable; dataGridView1.DataSource = bSource; 

it would be better if you recall your table

0
source

Source: https://habr.com/ru/post/1416056/


All Articles