Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

.net - Populating a listview multi-column

Regarding Listbox to ListView migration.

Hello.

I have a Listbox I add entries like this to:

1;content

Where 1 is always an int and content is always a string. I can access each one seperately.

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:

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.

How would I prepare and populate a listview in c# .net 4 for that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To set the ListView into Details mode:

        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.

To set automatic sorting of items:

        listView1.Sorting = SortOrder.Descending;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...