why doesn't datagridview refresh?

心不动则不痛 提交于 2019-12-30 06:45:44

问题


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?


回答1:


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 });



回答2:


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)
}



回答3:


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.)




回答4:


try this?

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



回答5:


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);



回答6:


Did you try calling EndEdit() before Refresh() ?




回答7:


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;



回答8:


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();



回答9:


Are you initializing the datasource the first time in an if(!Page.IsPostback)

It may be reseting the datasource on each postback.



来源:https://stackoverflow.com/questions/4108006/why-doesnt-datagridview-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!