Populating a listview multi-column

后端 未结 4 846
迷失自我
迷失自我 2020-12-05 16: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 i

相关标签:
4条回答
  • 2020-12-05 16:56

    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
    
    0 讨论(0)
  • 2020-12-05 17:02

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

    GridView

    0 讨论(0)
  • 2020-12-05 17:06

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

    0 讨论(0)
  • 2020-12-05 17:18

    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;
    
    0 讨论(0)
提交回复
热议问题