vb.net VB - 如何在列表视图的顶部动态添加项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19030741/
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 - How to dynamically add items on TOP of listview
提问by HelpASisterOut
I have a listview in VB.net that I'm filling from a table in my SQL database. The listview refreshes every period of time (using a timer) and I want every dynamically added item to be added on the TOPof the listview.
我在 VB.net 中有一个列表视图,我正在从我的 SQL 数据库中的一个表中填充它。列表视图每隔一段时间刷新一次(使用计时器),我希望将每个动态添加的项目添加到列表视图的顶部。
Here's my code:
这是我的代码:
Dim itm as Listviewitem
arr(0) = Date.Now.ToString
arr(1) = Table.item("no")
arr(2) = Table.item("datain")
arr(3) = Table.item("message")
itm = New ListViewItem(arr)
ListView1.Items.Add(itm)
Any idea how to do this?
知道如何做到这一点吗?
采纳答案by Karl Anderson
Use the Insertmethod instead of Add, like this:
使用Insert方法代替Add,如下所示:
ListView1.Items.Insert(0, itm)
Note: 0 is the index of the first item in the list, so this puts it at the beginning.
注意:0 是列表中第一项的索引,因此将其放在开头。
回答by Nunners
Instead of simply adding the item to the list, use the insert function :
不是简单地将项目添加到列表中,而是使用插入功能:
ListView1.Items.Insert(0, itm)

