gpt4 book ai didi

c# - 为什么datagridview不刷新?

转载 作者:太空狗 更新时间:2023-10-29 22:20:41 30 4
gpt4 key购买 nike

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

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

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

第一次尝试成功。

然而我第二次按下它时,它显示同样的东西!

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

最佳答案

此处与@Fake 有细微差别,但调用 Refresh() 将不起作用,因为仅在 dataGridView 上调用它

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

由于此方法与任何控件相关,与对象相关数据的刷新无关。引用here (DataGridView Methods)向下滚动到 Refresh,您将看到链接指向 Control.Refresh Method

你想要这样的东西;

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

然后您可以在 bs(您的 BindingSource)上调用 ResetBindings()

BindingSource bs = new BindingSource(); 
private refreshData()
{
bs.ResetBindings(false)
}

关于c# - 为什么datagridview不刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4108006/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com