Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why doesn't datagridview refresh?

here is what happens after i press a button:

    dataGridView1.DataSource = ConnectandReadList(some_query);
    dataGridView1.Refresh();

please note that i am doing this with another control called chart1 and it works fine with it, it populates it with the new requeried data, but datagridview is just staying the same

the first attempt is successful.

however the second time i press it, it display the same thing!

anyone know whether i am refreshing the datagridview correctly?

like image 348
Alex Gordon Avatar asked Nov 05 '10 16:11

Alex Gordon


People also ask

How do you refresh or show immediately in DataGridView after inserting?

by adding grdPatient. Update(); and grdPatient.

How to refresh DataGridView in c# after delete?

When the Delete Button is clicked, a Confirmation MessageBox will be displayed and if the User clicks Yes button the Row will be deleted (removed) from DataGridView. Then the DataGridView will be refreshed by again populating fresh data from the Database in Windows (WinForms) Application using C# and VB.Net.


3 Answers

Subtle difference here to @Fake but calling Refresh() won't work as calling this on the dataGridView only

"Forces the control to invalidate its client area and immediately redraw itself and any child controls."

As this method relates to any control, not to the refresh of the data relating to an object. Refer here (DataGridView Methods) and scroll down to Refresh and you will see the link points to Control.Refresh Method

You want something like this;

BindingSource bs = new BindingSource(); 
bs.DataSource = ConnectandReadList(some_query);
dataGridView1.DataSource = bs;
bs.ResetBindings(false)

and then you can just call ResetBindings() on bs (Your BindingSource);

BindingSource bs = new BindingSource(); 
private refreshData()
{
    bs.ResetBindings(false)
}
like image 71
Coops Avatar answered Oct 07 '22 07:10

Coops


Little trick you can do if you are binding to a List<> when setting it to your data source add ToList() on it at the end like this:

dataGridView1.DataSource = ConnectandReadList(some_query).ToList();

For some reason this causes it to refresh without loosing any references or anything.

An alternative is to directly notify the DataGridView that its data changed like this:

dataGridView1.DataSource = ConnectandReadList(some_query)
var m = dataGridView1.GetType().GetMethod("OnDataSourceChanged", BindingFlags.NonPublic | BindingFlags.Instance);
m.Invoke(dataGridView1, new object[] { EventArgs.Empty });
like image 21
SubqueryCrunch Avatar answered Oct 07 '22 05:10

SubqueryCrunch


A DataGridView sets up bindings the first time you assign the DataSource. The problem is that subsequent DataSource assignments, if the assignments have a different structure from the initial assignment, will fail because the bindings are now "off"

You need to reset the DataGridView thusly so that the data is bound a new. (The link is for VB but you just need to know the methods to call. Even copy/paste would be overkill.)

like image 36
Paul Sasik Avatar answered Oct 07 '22 05:10

Paul Sasik