0

我有一个 datagridview 和一个组合框,它们随机填充数据。但是,新数据永远不会自动显示。有人提到了使控件无效以强制应用程序重绘它并因此迭代内容并用新数据填充控件的想法。

有谁知道在 Windows 窗体应用程序中实现自动更新控件的最佳方法是什么?

非常感谢您的帮助,

问候。

4

5 回答 5

0

If you are talking about a control that gets populated with data from a database outside of your application, then there is no way for the control to know that the data has changed unless you re-query the database.

If on the other hand you have an application that is doing the data changes itself, then you can implement the INotifyPropertyChanged interface which means you can have a PropertyChanged event that your control reacts to so it updates when something changes. The following page has an example of implementing this: http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx

于 2009-07-03T09:26:37.797 回答
0

我想你使用数据绑定?您对自定义对象使用数据绑定?

在这种情况下,您的类必须实现 INotifyPropertyChanged 接口。

于 2009-07-03T08:49:39.203 回答
0

通常设置datagridview的datasource属性就足以导致datagridview自身失效并重绘。有时我记得我可能不得不先设置 datagridview.datasource = null 然后再次设置数据源以进行更新。

于 2009-07-03T08:50:39.950 回答
0

如果您使用 BindingSource,BindingSource.ResetBindings 方法可能会对您有所帮助。

于 2009-07-03T09:07:41.927 回答
0

有时,使控件无效并不总是有效。因为 WM_PAINT(底层 windows 消息)是消息队列中优先级最低的消息之一。因此,如果绘制请求过于频繁,Windows 会忽略它们。(这是我们在停止响应的应用程序中看到只有边框的“白色窗口”的另一个原因。为了避免这种情况,请调用 Application.DoEvents(); 这将强制队列处理所有事件,包括您重新绘制它的请求.

于 2009-07-03T09:14:18.013 回答