VB.net 列表视图填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22283477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.net Listview Populate
提问by Naffel
I'm trying to populate a Listview in VB.net with items from my access database.
我正在尝试使用访问数据库中的项目填充 VB.net 中的 Listview。
So far, i've managed to fill it with all the items I want, but I need to the items under the right columns. (User and Comment)
到目前为止,我已经设法用我想要的所有项目填充它,但我需要右列下的项目。(用户和评论)
Here's all the relevant code:
这是所有相关代码:
Dim count As Integer
Dim comments As New DataSet
comments = GetComments(classid)
With CommentsView
.View = View.Details
.Columns.Add("User")
.Columns.Add("Comment")
End With
count = CountRecords() - 1
For i As Integer = 0 To count
CommentsView.Items.Add(comments.Tables(0).Rows(i).Item(2))
CommentsView.Items.Add(comments.Tables(0).Rows(i).Item(3))
Next
So essentially I want Item(2) under Users and Item(3) under comments. Any ideas?
所以基本上我想要用户下的 Item(2) 和评论下的 Item(3) 。有任何想法吗?
Thanks.
谢谢。
回答by ??ssa P?ngj?rdenlarp
You are adding Items which equate to rows in a LV, you need to add User and Comment as SubItems (which visually equates to columns).
您正在添加相当于 LV 中的行的项目,您需要将用户和评论添加为子项目(在视觉上相当于列)。
Dim LVI as ListViewItem
' no need for a count temp var
For i As Integer = 0 To CountRecords() - 1
LVI = New ListViewItem
' whatever you want to show in columns 0
LVI.Text = (What_Ever_Text_For_Col_0)
' add subitem text
' this is adding strings from a dataset, but could be any string
LVI.SubItems.Add(comments.Tables(0).Rows(i).Item(2)) ' maybe .ToString?
LVI.SubItems.Add(comments.Tables(0).Rows(i).Item(3))
' add completed LVI to the LV
CommentsView.Items.Add(LVI)
Next

