I'm having trouble viewing the saved items in ListView.
The 1
is the Item No
and the c001
is the Item Code
but:
What's wrong with my code?
ListView1.Items.Clear()
sql = "SELECT * FROM inventory"
Try
dbcomm = New MySqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader()
While dbread.Read
ListView1.Items.Add(dbread("itemNo"))
ListView1.Items.Add(dbread("itemCode"))
End While
dbread.Close()
Catch ex As Exception
MsgBox("Error in collecting data from Database. Error is :" & ex.Message)
dbread.Close()
Exit Sub
End Try
You should add your second item in your SubItems
rather than Items
. Change this:
While dbread.Read
ListView1.Items.Add(dbread("itemNo")) 'placed in row #1
ListView1.Items.Add(dbread("itemCode")) 'placed in row #2
End While
Into:
While dbread.Read
ListView1.Items.Add(dbread("itemNo")).SubItems.Add(dbread("itemCode")) 'both placed in row #1
End While
This way, you place them side-by-side. Not one row after another.
Each time you call Items.Add
it adds a completely new item (it also returns a reference to the item you added)
What you need to do is to set the SubItem
text of each item you add. You can use the returned item reference to do this like so (requires Option Infer On
):
While dbread.Read
Dim lvi = ListView1.Items.Add(dbread("itemNo"))
lvi.SubItems.Add(dbread("itemCode"))
End While
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.