2

这是我填充列表视图的代码:

    Dim itms As ListViewItem
    Dim itm As New List(Of ListViewItem)
    Dim itemcoll(2) As String

    Dim strQ As String = String.Empty
    strQ = "SELECT COLOR_CODE,DESC from COLORS"
    cmd = New MySqlCommand(strQ, con)
    Try
        con.Open()
        rs = cmd.ExecuteReader
        lstview.Items.Clear()
        Application.DoEvents()
        lstview.SuspendLayout()
        lstview.BeginUpdate()
        lstview.Visible = False
        While rs.Read
            itemcoll(0) = IIf(Not IsDBNull(rs.Item("COLOR_CODE")), rs.Item("COLOR_CODE"), 0)
            itemcoll(1) = IIf(Not IsDBNull(rs.Item("DESC")), rs.Item("DESC"), 0)

            itms = New ListViewItem(itemcoll)
            'lstview.Items.Add(itms)
            itm.Add(itms)
        End While
        rs.Close()
        lstview.Items.AddRange(itm.ToArray)
        lstview.EndUpdate()
        lstview.Visible = True
        lstview.ResumeLayout()


    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        con.Close()
    End Try

此代码用于填充 10,000+ 行的速度很快,但如果我填充超过 50,000 行,则速度很慢,填充列表视图需要 2 秒,速度是正常还是慢?

还有哪些其他技术可以加快填充速度?我已经使用了一些方法来增加填充速度。

谢谢您的帮助。

4

1 回答 1

0

试试这个样本。有时不必要地写入占位符变量会降低性能,尽管理论上执行时间应该没有太大差异。

Dim itm As New List(Of ListViewItem)
Dim strQ As String = "SELECT COLOR_CODE,DESC from COLORS"
cmd = New MySqlCommand(strQ, con)


lstview.Items.Clear()
Application.DoEvents()
lstview.SuspendLayout()
lstview.BeginUpdate()
lstview.Visible = False

Try
    con.Open()

    rs = cmd.ExecuteReader
    While rs.Read            
        itm.Add(New ListViewItem({Convert.ToString(rs.Item("COLOR_CODE")),
                                 Convert.ToString(rs.Item("DESC"))}))
    End While
Catch ex As Exception
    MsgBox(ex.Message)
Finally
    con.Close()

    lstview.Items.AddRange(itm)
    lstview.EndUpdate()
    lstview.Visible = True
    lstview.ResumeLayout()
End Try
于 2013-08-28T04:25:12.147 回答