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?
by adding grdPatient. Update(); and grdPatient.
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.
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)
}
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 });
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With