Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 904
Donatus Avatar asked Jul 14 '12 09:07

Donatus


People also ask

What is ListView in VB net?

The ListView control is used to display a list of items. Along with the TreeView control, it allows you to create a Windows Explorer like interface. Let's click on a ListView control from the Toolbox and place it on the form. The ListView control displays a list of items along with icons.


2 Answers

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;
like image 141
hmqcnoesy Avatar answered Sep 17 '22 14:09

hmqcnoesy


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.

Hope this helps, if not feel free to let me know :)

like image 42
Sam Jenkins Avatar answered Sep 20 '22 14:09

Sam Jenkins