vb.net - Listview 实例子项
问题描述
我的目标是从 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
解决方案
好吧,在查看了我在此处发布的自己的问题后,我有了一个想法。 我想到了:
您只需要定义第二个子项.. DUH!
'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)
推荐阅读
- azure-application-insights - 可以从新的 Metrics Explorer 跳转到 Search?
- asp.net - 如何使用 ASP.NET Core 2 Web 服务器创建推送通知
- android - 从消失后更改可见性后获取图像宽度
- php - 如何在foreach循环codeigniter面包屑中将前一个值添加到下一个href?
- php - 在 MySQL 中使用中间表查询
- botframework - Microsoft Bot Framework Tools 通过拖放控件创建机器人
- angular - Angular:捕捉最后一次点击功能的时间
- reactjs - 如何在快递服务器上开始反应店面(moovweb)
- java - 如果您对一个对象进行了锁定,那么您是否对它的所有方法都进行了锁定?
- recursion - 在 F# 中是否可以使用递归部分函数?