How to update C # dataGridView after update?

I have a dataGridView when I click on any row, an open form to update the row data, but after the update is completed, the update form closes, but the dataGridView data is not updated

How can i do this?

+16
c # datagridview
Aug 10 '11 at 9:10
source share
5 answers

BindingSource is the only way without resorting to a third-party ORM, it may seem like a long winder at first, but the benefits of one update method on BindingSource so useful.

If your source says, for example, a list of user strings

 List<string> users = GetUsers(); BindingSource source = new BindingSource(); source.DataSource = users dataGridView1.Datasource = source; 

when your editing just updates your data object, whether it be a DataTable or a list of user strings, like here, and ResetBindings on a BindingSource ;

 users = GetUsers(); //Update your data object source.ResetBindings(false); 
+30
Aug 16 2018-11-11T00:
source share
β€” -

Return your DatagridView to source.

 DataGridView dg1 = new DataGridView(); dg1.DataSource = src1; // Update Data in src1 dg1.DataSource = null; dg1.DataSource = src1; 
+29
Aug 10 '11 at 10:11
source share

I don’t know whether this was really decided or not ... but looking at all the other answers, nothing seems completely clear. The best way I found this is to put the same code that was used to populate your datagridview in the method and pass it to your datagridview form, like this:

 public void ConnectAndPopulateDataGridView(DataGridView dataGridView) { } 

The code inside the method is exactly the same as the code used to populate the datagirdview initially, except that the name of the datagridview changes to whatever you called in your method.

Now this method is called in your parent form.

The child view is launched through .ShowDialog() , after which the method is called after it is called immediately after the child is closed ... like this:

 ChildForm.ShowDialog(); ConnectAndPopulateDataGridView(dataGridView1); 
0
Mar 24 '16 at 20:34
source share

You just need to override the DataSource . So if you have a DataGridView DataSource that contains a, b, I c:

 DataGridView.DataSource = a, b, c 

And suddenly you update the DataSource to have only a and b, you will need to override your DataSource:

 DataGridView.DataSource = a, b 

I hope you find this helpful.

Thank.

-one
Dec 16 '14 at 13:14
source share

You can use SqlDataAdapter to update DataGridView

  using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Table", conn)) { DataTable dt = new DataTable(); ad.Fill(dt); dataGridView1.DataSource = dt; } } 
-one
Apr 01 '16 at 11:15
source share



All Articles