簡體   English   中英

Listview實例子項

[英]Listview Instance Subitems

我的目標是從OleDb查詢的輸出數據填充列表視圖。 我可以正確地填充項目和一個子項目。 但是我很難定義每個項目的其他4個子項目。

如您所知,通常使用

ListView1.Items(0).SubItems(1).Text = "Test Item 2"

但是我不知道如何處理在運行時填充Control Listview的Listview實例。

這是我的代碼,成功填充了一個項目和一個子項目:

    Dim conn As New System.Data.OleDb.OleDbConnection(connectionString)
    Dim com As System.Data.OleDb.OleDbCommand
    Dim reader As System.Data.OleDb.OleDbDataReader

    Try
        'Open the connection
        conn.Open()
        'Create a new instance of the command and provide the SELECT query, and the opened connection
        com = New System.Data.OleDb.OleDbCommand("SELECT * FROM Schedules", conn)
        reader = com.ExecuteReader(CommandBehavior.CloseConnection)

        'Check to see if the SELECT query returned any rows
        If reader.HasRows Then
            'If so, perform a read for each row
            While reader.Read
                'Declare a new ListViewItem, and provide the information
                'to be shown in the very first column
                Dim item As New ListViewItem(reader.Item("colName").ToString)
                'Decare a new ListViewSubItem, and provide the information
                'to be shown in the second (and so forth) column
                Dim subItem As New ListViewItem.ListViewSubItem
                subItem.Text = reader.Item("colNextRun").ToString

                'Ideally, I'd like to add "colLastRun as another sub item
                'subItem1.Text = reader.Item("colLastRun").ToString

                'Add the ListViewSubItem to the ListViewItem
                item.SubItems.Add(subItem)
                'Add the ListViewItem to the ListView
                lv.Items.Add(item)
                'Repeat until all rows have been read
            End While
        End If
        'Close the reader
        reader.Close()

    Catch ex As Exception
        'If something went sideways, make sure that you close the connection
        'before exiting the method.
        If conn.State = ConnectionState.Open Then
            'MsgBox(ex.Message)
            conn.Close()
        End If
    End Try

好吧,我回顧了我在這里發布的問題后,有了一個主意。 我想到了:

您只需要定義第二個子項即可。

                'Decare a new ListViewSubItem, and provide the information
                'to be shown in the second (and so forth) column
                Dim subItem As New ListViewItem.ListViewSubItem
                Dim subItem1 As New ListViewItem.ListViewSubItem()
                Dim subItem2 As New ListViewItem.ListViewSubItem
                subItem.Text = reader.Item("colNextRun").ToString
                subItem1.Text = reader.Item("colLastRun").ToString
                subItem2.Text = reader.Item("colFileLocation").ToString
                'Add the ListViewSubItem to the ListViewItem
                item.SubItems.Add(subItem)
                item.SubItems.Add(subItem1)
                item.SubItems.Add(subItem2)

祝賀您找出解決方案。 您在連接方面做得非常好,但是不僅需要關閉連接,而且還必須處理掉連接。 查看使用可為您兼顧的模塊。 即使出現錯誤,它們也會關閉並處置您的數據庫對象。

您無需為每個子項目創建一個子項目對象。 子項集合的.Add方法可以使用一個字符串,該字符串將提供該子項的文本。 在內部,它將創建子項並將其添加到集合中。

請注意在ListView上使用.BeginUpdate.EndUpdate 如果要添加許多項目,這將大大加快速度。 它可以防止控件在每次添加時重新繪制。 完成添加所有項目后,它將重新繪制一次。

Private Sub FillListView()
    Using conn As New OleDbConnection("Your connection string")
        Using cmd As New OleDbCommand("SELECT * FROM Schedules", conn)
            conn.Open()
            Using reader = cmd.ExecuteReader
                ListView1.BeginUpdate()
                While reader.Read
                    Dim item As New ListViewItem(reader.Item("colName").ToString)
                    item.SubItems.Add(reader.Item("NextcolName").ToString)
                    item.SubItems.Add(reader.Item("NextcolName").ToString)
                    item.SubItems.Add(reader.Item("NextcolName").ToString)
                    ListView1.Items.Add(item)
                End While
                ListView1.EndUpdate()
            End Using
        End Using
    End Using
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM