C# ListView Column Width Auto

后端 未结 10 1166
傲寒
傲寒 2020-11-28 07:04

How can I set the column width of a c# winforms listview control to auto. Something like width = -1 / -2 ?

10条回答
  •  误落风尘
    2020-11-28 08:02

    It is also worth noting that ListView may not display as expected without first changing the property:

    myListView.View = View.Details; // or View.List
    

    For me Visual Studio seems to default it to View.LargeIcon for some reason so nothing appears until it is changed.

    Complete code to show a single column in a ListView and allow space for a vertical scroll bar.

    lisSerials.Items.Clear();
    lisSerials.View = View.Details;
    lisSerials.FullRowSelect = true;
    
    // add column if not already present
    if(lisSerials.Columns.Count==0)
    {
        int vw = SystemInformation.VerticalScrollBarWidth;
        lisSerials.Columns.Add("Serial Numbers", lisSerials.Width-vw-5);
    }
    
    foreach (string s in stringArray)
    {
        ListViewItem lvi = new ListViewItem(new string[] { s });
        lisSerials.Items.Add(lvi);
    }
    

提交回复
热议问题