Populating a listview multi-column
Asked Answered
L

5

15

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?

Leonard answered 14/7, 2012 at 9:14 Comment(0)
F
46

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;
Frulla answered 14/7, 2012 at 18:44 Comment(3)
Thanks you SO much! This is the ONLY answer I've found that actually makes simple sense! Exactly what I needed. (I'm filling a listview from an XML) You're a hero to me now. Take my upvote :)Domicile
Also for formatting you can use listView1.GridLines = true;, alignment listView1.Columns[0].TextAlign = HorizontalAlignment.Center;. For column widths see here: #1258000.Kristiekristien
Works like a charm, finally found the answer after so long time haha, thank you man ♥Benildas
E
1

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 :)

Erewhile answered 21/4, 2013 at 8:52 Comment(0)
F
1

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
Foreshorten answered 13/11, 2013 at 16:18 Comment(1)
This is tagged c# though, not VB.Bourg
R
0

Form with multi column populated listview

Here is the full form code-Compile it from command window::

Imports System.Windows.Forms Imports System.Drawing Public Class Form1 Inherits System.Windows.Forms.Form

  Public ListView1 As ListView
  Public Col_Header1 As ColumnHeader
  Public Col_Header2 As ColumnHeader
  Public Col_Header3 As ColumnHeader
  Public Col_Header4 As ColumnHeader

  Public Sub New()
    ListView1 = New ListView
    Col_Header1 = New ColumnHeader
    Col_Header2 = New ColumnHeader
    Col_Header3 = New ColumnHeader
    Col_Header4 = New ColumnHeader
    ListView1.Columns.AddRange(New ColumnHeader() { _
                                       Col_Header1, _
                                       Col_Header2, _
                                       Col_Header3, _
                                       Col_Header4})
        Col_Header1.Text = "Header1"
        Col_Header1.Width = 200
        Col_Header2.Text = "Header2"
        Col_Header2.Width = 200
        Col_Header3.Text = "Header3"
        Col_Header3.Width = 200
        Col_Header4.Text = "Header4"
        Col_Header4.Width = 400
   Dim myItems As String() = New String() { _
                            "Item1", _
                            "Item2", _
                            "Item3", _
                            "Item4"}
   Dim myItems1 As String() = New String() { _
                            "Item11", _
                            "Item12", _
                            "Item13", _
                            "Item14"}
   Dim myItems2 As String() = New String() { _
                            "Item21", _
                            "Item22", _
                            "Item23", _
                            "Item24"}
   Dim myItems3 As String() = New String() { _
                            "Item31", _
                            "Item32", _
                            "Item33", _
                            "Item34"}
   Dim myItems4 As String() = New String() { _
                            "Item41", _
                            "Item42", _
                            "Item43", _
                            "Item44"}
           Dim lvi As ListViewItem = New ListViewItem(myItems)
           ListView1.Items.Add(lvi)
           Dim lvi1 As ListViewItem = New ListViewItem(myItems1)
           ListView1.Items.Add(lvi1)
           Dim lvi2 As ListViewItem = New ListViewItem(myItems2)
           ListView1.Items.Add(lvi2)
           Dim lvi3 As ListViewItem = New ListViewItem(myItems3)
           ListView1.Items.Add(lvi3)
           Dim lvi4 As ListViewItem = New ListViewItem(myItems4)
           ListView1.Items.Add(lvi4)
        ListView1.AllowColumnReorder = True
        ListView1.Font = New System.Drawing.Font("Segoe UI",15.0!, _
                       System.Drawing.FontStyle.Bold, System.Drawing. _
                                     GraphicsUnit.Point,CType(0, Byte))
        ListView1.FullRowSelect = True
        ListView1.HideSelection = False
        ListView1.Dock = DockStyle.Fill
        ListView1.Name = "listView1"            
        ListView1.View = View.Details
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(800,600)
        Me.Controls.Add(ListView1)
        Me.Text = "Deep_Research_For_ListView_Developement"
        Me.Name = "Research_Form1"            
        Me.ResumeLayout(False)
    End Sub

    Public Shared Sub Main()
      Dim form1 As Form1 = New Form1()
      form1.ShowDialog()
    End Sub 

    Private Sub Research_Form1_Load(ByVal sender As Object, _
                         ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = New System.Drawing.Point(0,0)

'''###########################-Both Codes work-########################### ' For Each i As ListViewItem In listView1.Items ' i.ForeColor = Color.Green ' i.BackColor = Color.Yellow
' Next

            For Each i As ListViewItem In ListView1.Items
              If (i.Index Mod 2) = 1 Then
                 i.BackColor = Color.FromArgb(230, 150, 255)
                 i.UseItemStyleForSubItems = True
              End If
            Next

'''###########################-Both Codes work-########################### End Sub End Class

Compiler Call From CMD:-

vbc.exe -target:exe [path]\Form1.vb [path]\Form1.exe

Refuel answered 21/4, 2021 at 12:40 Comment(0)
C
-2

Classic use us ListView GridView. The GridView is what add the columns.

GridView

Ceres answered 14/7, 2012 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.