简体   繁体   English

列表视图vbnet

[英]List View vbnet

I'm having trouble viewing the saved items in ListView. 我在查看ListView中保存的项目时遇到问题。

The 1 is the Item No and the c001 is the Item Code but: 1Item Noc001Item Code但是:

在此输入图像描述

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 . 您应该在SubItems而不是Items添加第二个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) 每次调用Items.Add它都会添加一个全新的项目(它还会返回对您添加的项目的引用)

What you need to do is to set the SubItem text of each item you add. 您需要做的是设置您添加的每个项目的SubItem文本。 You can use the returned item reference to do this like so (requires Option Infer On ): 您可以使用返回的项目引用来执行此操作(需要Option Infer On ):

    While dbread.Read
        Dim lvi = ListView1.Items.Add(dbread("itemNo"))
        lvi.SubItems.Add(dbread("itemCode"))
    End While

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM