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?
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.)
Did you try calling EndEdit()
before Refresh()
?
You could do as @cycl suggested, you could also use a BindingSource which I believe is the recommended method by microsoft.
BindingSource bs = new BindingSource();
bs.DataSource = ConnectandReadList(some_query);
dataGridView1.DataSource = bs;
dataGridView1.Refresh;
Actually problem is here that your TableAdapter is not refreshing. I used this code after editing table (mydatabase.tbl) to refresh it:
tblTableAdapter.Fill(mydatabaseDataSet.tbl);
dataGridView1.DataSource = tblBindingSource;
dataGridView1.Update();
This line of code loads data into the wMP_EXPORTDataSet.DEST_AX_PRICEDISCADMTRANSENTITY
table. You can move, or remove it, as needed.
this.dEST_AX_PRICEDISCADMTRANSENTITYTableAdapter.Fill(this.wMP_EXPORTDataSet.DEST_AX_PRICEDISCADMTRANSENTITY);
Are you initializing the datasource the first time in an if(!Page.IsPostback)
It may be reseting the datasource on each postback.
try this?
dataGridView1.DataSource = null;
dataGridView1.DataSource = ConnectandReadList(some_query);
dataGridView1.Refresh();
精彩评论