我想问一下如何使用vb.net中的计时器自动刷新/更新listview ..给我一个想法或步骤或代码。这是我在计时器中的代码:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn.Open()
Timer1.enabled = False
Timer1.interval = 5000
Dim strquery As String = "Select * " & _
"from software "
Dim myCommand As New OdbcCommand(strquery, conn)
Dim myReader As OdbcDataReader
myReader = myCommand.ExecuteReader
ListView1.Items.Clear()
If myReader.HasRows = True Then
'ListView1.BeginUpdate()
While myReader.Read
Dim ListView As ListViewItem
ListView = ListView1.Items.Add(myReader("log_type").ToString())
ListView.SubItems.Add(myReader("log_desc").ToString())
ListView.SubItems.Add(myReader("log_details").ToString())
ListView.SubItems.Add(myReader("log_date").ToString())
ListView.SubItems.Add(myReader("software_desc").ToString())
ListView.SubItems.Add(myReader("software_name").ToString())
ListView.SubItems.Add(myReader("develop_date").ToString())
ListView.SubItems.Add(myReader("last_update").ToString())
End While
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = 1000
Timer1.Enabled = False
ListView1.Refresh()
ListView1.Items.Add(DateTime.Now.ToLongTimeString() + "," + _
DateTime.Now.ToLongDateString())
End Sub
此计时器不起作用。有什么方法可以自动更新或自动刷新列表视图吗?
答案 0 :(得分:1)
您需要在最后再次启用定时器:
Timer1.Enabled = True
如果没有,计时器将无法工作。 Timer1.Enabled = False
取消激活计时器。