C# ListView 列宽自动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1257500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:19:36  来源:igfitidea点击:

C# ListView Column Width Auto

c#.netwinformslistviewwidth

提问by Kai

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

如何将 ac# winformslistview控件的列宽设置为自动。像宽度 = -1 / -2 之类的东西?

采纳答案by Fredrik M?rk

You gave the answer: -2 will autosize the column to the length of the text in the column header, -1 will autosize to the longest item in the column. All according to MSDN. Note though that in the case of -1, you will need to set the column width after adding the item(s). So if you add a new item, you will also need to assign the width property of the column (or columns) that you want to autosize according to data in ListViewcontrol.

您给出了答案:-2 会将列自动调整为列标题中文本的长度,-1 将自动调整为列中最长的项目。所有根据 MSDN。请注意,在 -1 的情况下,您需要在添加项目后设置列宽。因此,如果您添加新项目,您还需要根据ListView控制中的数据分配要自动调整大小的列(或多列)的宽度属性。

回答by Jimmy Campbell

I made a program that cleared and refilled my listview multiple times. For some reason whenever I added columns with width = -2 I encountered a problem with the first column being way too long. What I did to fix this was create this method.

我制作了一个程序,可以多次清除并重新填充我的列表视图。出于某种原因,每当我添加宽度为 -2 的列时,我都会遇到第一列太长的问题。我为解决此问题所做的是创建此方法。

private void ResizeListViewColumns(ListView lv)
{
    foreach(ColumnHeader column in lv.Columns)
    {
        column.Width = -2;
    }
}

The great thing about this method is that you can pretty much put this anywhere to resize all your columns. Just pass in your ListView.

这种方法的好处在于您几乎可以将它放在任何地方来调整所有列的大小。只需传入您的ListView.

回答by Majid

Use this:

用这个:

yourListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
yourListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

from here

这里

回答by Joe

Expanding a bit on Fredrik's answer, if you want to set the column's auto-resize width on the fly for example: setting the first column's auto-size width to 70:

扩展一下 Fredrik 的答案,如果您想即时设置列的自动调整宽度,例如:将第一列的自动调整宽度设置为 70:

myListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.None);
myListView.Columns[0].Width = 70;
myListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);

回答by Niclas Lindstedt

This solution will first resize the columns based on column data, if the resized width is smaller than header size, it will resize columns to at least fit the header. This is a pretty ugly solution, but it works.

此解决方案将首先根据列数据调整列的大小,如果调整后的宽度小于标题大小,它将调整列的大小以至少适合标题。这是一个非常丑陋的解决方案,但它有效。

lstContacts.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
colFirstName.Width = (colFirstName.Width < 60 ? 60 : colFirstName.Width);
colLastName.Width = (colLastName.Width < 61 ? 61 : colLastName.Width);
colPhoneNumber.Width = (colPhoneNumber.Width < 81 ? 81 : colPhoneNumber.Width);
colEmail.Width = (colEmail.Width < 40 ? 40 : colEmail.Width);

lstContacts is the ListView. colFirstName is a column, where 60 is the width required to fit the title. Etc.

lstContacts 是 ListView。colFirstName 是一列,其中 60 是适合标题所需的宽度。等等。

回答by Jason

I believe the author was looking for an equivalent method via the IDE that would generate the code behind and make sure all parameters were in place, etc. Found this from MS:

我相信作者正在通过 IDE 寻找一种等效的方法,该方法会生成背后的代码并确保所有参数都已就位等。 从 MS 找到了这个:

Creating Event Handlers on the Windows Forms Designer

在 Windows 窗体设计器上创建事件处理程序

Coming from a VB background myself, this is what I was looking for, here is the brief version for the click adverse:

我自己来自 VB 背景,这就是我正在寻找的,这里是点击不利的简短版本:

  1. Click the form or control that you want to create an event handler for.
  2. In the Properties window, click the Events button
  3. In the list of available events, click the event that you want to create an event handler for.
  4. In the box to the right of the event name, type the name of the handler and press ENTER
  1. 单击要为其创建事件处理程序的窗体或控件。
  2. 在“属性”窗口中,单击“事件”按钮
  3. 在可用事件列表中,单击要为其创建事件处理程序的事件。
  4. 在事件名称右侧的框中,键入处理程序的名称并按 ENTER

回答by Nickyboy

You can use something like this, passing the ListView you want in param

你可以使用这样的东西,在 param 中传递你想要的 ListView

    private void AutoSizeColumnList(ListView listView)
    {
        //Prevents flickering
        listView.BeginUpdate();

        Dictionary<int, int> columnSize = new Dictionary<int,int>();

        //Auto size using header
        listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

        //Grab column size based on header
        foreach(ColumnHeader colHeader in listView.Columns )
            columnSize.Add(colHeader.Index, colHeader.Width);

        //Auto size using data
        listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

        //Grab comumn size based on data and set max width
        foreach (ColumnHeader colHeader in listView.Columns)
        {
            int nColWidth;
            if (columnSize.TryGetValue(colHeader.Index, out nColWidth))
                colHeader.Width = Math.Max(nColWidth, colHeader.Width);
            else
                //Default to 50
                colHeader.Width = Math.Max(50, colHeader.Width);
        }

        listView.EndUpdate();
    }

回答by David Silva-Barrera

There is another useful method called AutoResizeColumnwhich allows you to auto size a specific column with the required parameter.

还有另一种有用的方法AutoResizeColumn,它允许您使用所需的参数自动调整特定列的大小。

You can call it like this:

你可以这样称呼它:

listview1.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent);
listview1.AutoResizeColumn(2, ColumnHeaderAutoResizeStyle.ColumnContent);
listview1.AutoResizeColumn(3, ColumnHeaderAutoResizeStyle.HeaderSize);
listview1.AutoResizeColumn(4, ColumnHeaderAutoResizeStyle.HeaderSize);

回答by tonyb

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

还值得注意的是,如果不先更改属性,ListView 可能无法按预期显示:

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.

对我来说,Visual Studio 似乎出于某种原因将其默认为 View.LargeIcon,因此在更改之前不会出现任何内容。

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

在 ListView 中显示单个列并为垂直滚动条留出空间的完整代码。

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);
}

回答by Tomá? Krása

If you have ListView in any Parent panel (ListView dock fill), you can use simply method...

如果您在任何父面板(ListView 停靠栏填充)中有 ListView,则可以使用简单的方法...

private void ListViewHeaderWidth() {
        int HeaderWidth = (listViewInfo.Parent.Width - 2) / listViewInfo.Columns.Count;
        foreach (ColumnHeader header in listViewInfo.Columns)
        {
            header.Width = HeaderWidth;
        }
    }