简体   繁体   中英

How to reload list view?

I have run into a problem. When the main form load, it call the 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

It works good !. But I want to update the database from another form and then close the window and update the listview on the original page. I write loadSentbox() in the close event, but listview shows nothing. What is the solution ? (i'm new to vb.net. Dont write a complex code)

Try this code
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 = New SqlClient.SqlCommand(strcmd, con)
dr1 = cmd.ExecuteReader
While (dr1.Read())
item = New ListViewItem()
item.Text = dr1(0).ToString
i = 1
While i < dr1.FieldCount
item.SubItems.Add(dr1(i).ToString)
i = i + 1
End While
list.Items.Add(item)
End While
dr1.Close()
con.Close()
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM