.net 填充列表视图多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11482501/
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
Populating a listview multi-column
提问by Donatus
Regarding Listbox to ListView migration.
关于 Listbox 到 ListView 的迁移。
Hello.
你好。
I have a Listbox I add entries like this to:
我有一个列表框,我将这样的条目添加到:
1;content
1;内容
Where 1 is always an int and content is always a string. I can access each one seperately.
其中 1 始终是 int 而 content 始终是字符串。我可以单独访问每一个。
Now I want the result to be sorted descendingly, ie:
现在我希望结果按降序排序,即:
1;content
4;content2
2;content3
=>
=>
4;content2
2;content3
1;content
As this doesn't look good, I want to use a Listview instead. Like this:
由于这看起来不太好,我想改用 Listview。像这样:
Frequency | Content
===============
4 | content2
2 | content3
1 | content
Problem is, the tabular property does not seem to exist, all entries are being put in like symbols in a listview in explorer. Also I have problems "reaching" the 2nd column(content), ie I only see 4,2,1.
问题是,表格属性似乎不存在,所有条目都像符号一样放在资源管理器的列表视图中。我也有问题“到达”第二列(内容),即我只看到 4,2,1。
How would I prepare and populate a listview in c# .net 4 for that?
我将如何为此准备和填充 c# .net 4 中的列表视图?
回答by hmqcnoesy
To set the ListView into Details mode:
要将 ListView 设置为详细信息模式:
listView1.View = View.Details;
Then to set up your two columns:
然后设置你的两列:
listView1.Columns.Add("Frequency");
listView1.Columns.Add("Content");
Then to add your items:
然后添加您的项目:
listView1.Items.Add(new ListViewItem(new string[]{"1", "content"}));
listView1.Items.Add(new ListViewItem(new string[]{"4", "content2"}));
listView1.Items.Add(new ListViewItem(new string[]{"2", "content3"}));
I chose to use the overload of the ListViewItem constructor that takes a string array representing the column values. But there are 22 overloads! Look through then and find the one that fits your situation best.
我选择使用 ListViewItem 构造函数的重载,该构造函数采用表示列值的字符串数组。但是有 22 个重载!然后仔细查看,找到最适合您情况的那个。
To set automatic sorting of items:
要设置项目的自动排序:
listView1.Sorting = SortOrder.Descending;
回答by Satal
I realise that this post is over a year old but I thought this may be of use, I wrote an article years ago about using a ListView as a multicolumn ListBox, which includes code for populating it. The article is available here (Using a ListView as a multicolumn ListBox) it is written using VB.NET but the code is pretty much exactly the same for C#, I may rewrite it using C# and will add a link for that but that'll be another time.
我意识到这篇文章已经有一年多了,但我认为这可能有用,我几年前写了一篇关于使用 ListView 作为多列 ListBox 的文章,其中包括填充它的代码。这篇文章在这里可用(使用 ListView 作为多列 ListBox)它是使用 VB.NET 编写的,但代码与 C# 几乎完全相同,我可能会使用 C# 重写它,并会为此添加一个链接,但这会是另一个时间。
Hope this helps, if not feel free to let me know :)
希望这会有所帮助,如果不放心,请告诉我:)
回答by sri
To add the list view headers and add items to list view, try this code:
要添加列表视图标题并将项目添加到列表视图,请尝试以下代码:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Lstv1.Columns.Add("Paramname", CInt(Lstv1.Width / 2))
Lstv1.Columns.Add("Paramorder", CInt(Lstv1.Width / 2))
End Sub
Private Sub appendlistview(ByVal Paramname As String, ByVal Paramorder As String)
Dim newitem As New ListViewItem(Paramname)
newitem.SubItems.Add(Paramorder)
Lstv1.Items.Add(newitem)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
Call appendlistview(TextBox1.Text, TextBox2.Text)
End Sub

