I have a goal of populating a listview from the output data of a OleDb query. I can correctly populate the Item and one subitem with no issue. But I'm having trouble defining the other 4 subitems of each item.
As you know, typically you define subitems with
ListView1.Items(0).SubItems(1).Text = "Test Item 2"
But I can't figure out how to do that when working with an instance of a Listview which populates my Control Listview at runtime.
Here is the code I have, which sucessfully populates an item and one 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
Congratulations on figuring out a fix. You have done a very good job with your connection but connections need to be not only closed but disposed. Look into Using blocks which take care of both for you. They will close and dispose your database objects even if there is an error.
You don't need to create a subitems object for each subitem. The .Add method of the sub items collection can take a string which will provide the text for that subitem. Internally it will create the subitem and add it to the collection.
Note the use of .BeginUpdate
and .EndUpdate
on the ListView. If you are adding many items this will speed things up considerably. It prevents the control from repainting on each addition. It will repaint once when you finish adding all the items.
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