我的目标是从OLEDB查询的输出数据中填充ListView。我可以正确地填充该项目,没有任何问题。但是我在定义每个项目的其他4个子项目时遇到了麻烦。
如您所知,通常您会用
定义子目标ListView1.Items(0).SubItems(1).Text = "Test Item 2"
但是,在使用ListView的实例时,我无法弄清楚该如何做到这一点,该实例在运行时填充了我的控制列表视图。
这是我拥有的代码,它可以成功地填充一个项目和一个subitem:
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
恭喜您找出修复程序。您在连接方面做得很好,但是连接不仅需要封闭,而且需要处置。查看使用为您服务的块。即使存在错误,他们也会关闭并处理您的数据库对象。
您无需为每个子项目创建一个子项目对象。子项目集合的.ADD方法可以采用一个字符串,该字符串将为该subitem提供文本。在内部,它将创建subitem并将其添加到集合中。
请注意在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
好吧,我在回顾了自己在此处发布的问题后有了一个想法。我弄清楚了:
您只需要定义第二个子项.. du!
'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)