为什么datagridview不刷新? [英] why doesn't datagridview refresh?

查看:23
本文介绍了为什么datagridview不刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我按下按钮后发生的事情:

here is what happens after i press a button:

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

请注意,我正在使用另一个名为 chart1 的控件执行此操作,并且它可以正常工作,它会使用新的重新查询数据填充它,但是 datagridview 只是停留一样的

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

第一次尝试成功.

但是我第二次按下它时,它显示相同的内容!

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

有人知道我是否正确刷新了 datagridview 吗?

anyone know whether i am refreshing the datagridview correctly?

推荐答案

如果您绑定到 List<> 将它设置到数据源时,您可以做的小技巧在最后添加 ToList() 如下所示:

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.

另一种方法是直接通知 DataGridView 它的数据更改如下:

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

这篇关于为什么datagridview不刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆