每 3 秒动态自动刷新 datagridview vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27306152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:06:04  来源:igfitidea点击:

Dynamically auto refresh datagridview every 3 seconds vb.net

vb.netdatagridviewtimerrefresh

提问by wwrwer

In my form I have a datagridview which needs to be automatically refreshedevery 2 seconds without me having to close the application. I have used the following timer code to try and achieve that. I have placed this code in my form load which is also where my datagridview code is:

在我的表单中,我有一个 datagridview,它需要每 2 秒自动刷新一次,而不必关闭应用程序。我已经使用以下计时器代码来尝试实现这一目标。我已将此代码放在我的表单加载中,这也是我的 datagridview 代码所在的位置:

 Dim timer As New Timer()
 timer.Interval = 2000
 AddHandler timer.Tick, AddressOf timer_Tick
 timer.Start()

Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)

    Me.DataGridView1.Refresh()

End Sub

However, all it does is flicker and doesn't actually refresh the datagridview. My datagrid is connected to a Access database and is not binded, I did with SQL. What am I doing wrong?

但是,它所做的只是闪烁,实际上并没有刷新 datagridview。我的数据网格连接到 Access 数据库并且没有绑定,我用 SQL 做到了。我究竟做错了什么?

回答by OneFineDay

DGV.Refreshtell the the program to redraw the control. You need to rerun the process that gets your data and recall it.

DGV.Refresh告诉程序重绘控件。您需要重新运行获取数据并调用它的过程。

Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
  UpdateDGV()
End Sub

Private Sub UpdateDGV()
  'run sql stuff in here
End Sub