如何重新加载列表视图?

时间:2015-06-13 19:13:41

标签: mysql vb.net listview listbox refresh

我遇到了一个问题。当主窗体加载时,它调用loadSentnbox()

Public Sub loadSentbox()
    logmysql()
    Try

        Dim sqlquery As String
        logmysql()
        sqlquery = "SELECT * FROM files_tr WHERE `from` = '" + user + "'"
        mysqlCommand = New MySqlCommand(sqlquery, mysqlCon)
        'Open the Db
        mysqlCon.Open()
        mysqlReader = mysqlCommand.ExecuteReader
        While mysqlReader.Read
            Dim i As ListViewItem
            ltv_sentbox.BeginUpdate()
            i = ltv_sentbox.Items.Add(mysqlReader.Item("to"))
            i.SubItems.Add(mysqlReader.Item("filename"))
            i.SubItems.Add(mysqlReader.Item("size"))
            i.SubItems.Add(mysqlReader.Item("status"))
            ltv_inbox.Update()
            ltv_inbox.EndUpdate()
        End While
        mysqlCon.Close()
        'ltv_sentbox.Refresh()
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.Critical)
    End Try
End Sub

效果很好!但我想从另一个表单更新数据库,然后关闭窗口并更新原始页面上的列表视图。我在close事件中编写loadSentbox(),但listview什么也没显示。 解决办法是什么 ? (我是vb.net的新手。不要写复杂的代码)

1 个答案:

答案 0 :(得分:0)

试用此代码
Public Sub DatabaseToListview(ByVal strcon As String,ByVal strcmd As String,ByVal list as ListView)
list.Items.Clear()
Dim dr1 As SqlClient.SqlDataReader
Dim con As New SqlClient.SqlConnection(strcon)
Dim i As Integer = 0
Dim cmd As New SqlClient.SqlCommand
Dim item As New ListViewItem
con.Open()
cmd =新的SqlClient.SqlCommand(strcmd,con)
dr1 = cmd.ExecuteReader
while(dr1.Read())
item = New ListViewItem()
item.Text = dr1(0).ToString
i = 1
虽然我< dr1.FieldCount
item.SubItems.Add(dr1(i).ToString)
i = i + 1
结束时
list.Items.Add(item)
结束时
dr1.Close()
con.Close()
结束子