C# ListView 列宽自动

发布于 2024-07-30 11:26:07 字数 80 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

七七 2024-08-06 11:26:07

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

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 ListView control.

放低过去 2024-08-06 11:26:07

使用此:

yourListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

yourListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

来自此处

Use this:

yourListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

yourListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

from here

绿光 2024-08-06 11:26:07

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

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

此方法的优点在于您几乎可以将其放在任何地方来调整所有列的大小。 只需传入您的 ListView 即可。

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.

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.

找回味觉 2024-08-06 11:26:07

你可以使用这样的东西,在参数中传递你想要的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();
    }

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

    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();
    }
荒芜了季节 2024-08-06 11:26:07

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

你可以这样称呼它:

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

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

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);
北城孤痞 2024-08-06 11:26:07

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

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

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

private void ListViewHeaderWidth() {
        int HeaderWidth = (listViewInfo.Parent.Width - 2) / listViewInfo.Columns.Count;
        foreach (ColumnHeader header in listViewInfo.Columns)
        {
            header.Width = HeaderWidth;
        }
    }
弥繁 2024-08-06 11:26:07

如果您想动态设置列的自动调整宽度,请对 Fredrik 的答案进行一些扩展
例如:将第一列的自动调整宽度设置为 70:

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

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:

myListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.None);
myListView.Columns[0].Width = 70;
myListView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
初懵 2024-08-06 11:26:07

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

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

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

完整的代码以在 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);
}

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);
}
活雷疯 2024-08-06 11:26:07

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

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 是列表视图。
colFirstName 是一列,其中 60 是适合标题所需的宽度。
ETC。

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.

忆沫 2024-08-06 11:26:07

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

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

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

  1. 单击要为其创建事件处理程序的窗体或控件。
  2. 在“属性”窗口中,单击“事件”按钮
  3. 在可用事件列表中,单击要为其创建事件处理程序的事件。
  4. 在事件名称右侧的框中,键入处理程序的名称,然后按 Enter

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:

Creating Event Handlers on the Windows Forms Designer

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

  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
乖乖兔^ω^ 2024-08-06 11:26:07

我找不到合适的代码,所以我决定自己编写。
在这里你可以使用它。

public static int AutoResizeColumnEX_Value = 0;

public static void AutoResizeColumnEX(this ListView listView, int columnIndex)
{
    if (columnIndex != (listView.Columns.Count - 1))
    {
        var orginalEndColumnWidth = listView.Columns[listView.Columns.Count - 1].Width;

        listView.AutoResizeColumn(listView.Columns.Count - 1, ColumnHeaderAutoResizeStyle.ColumnContent);
        listView.AutoResizeColumn(listView.Columns.Count - 1, ColumnHeaderAutoResizeStyle.HeaderSize);

        if (orginalEndColumnWidth < listView.Columns[listView.Columns.Count - 1].Width)
        {
            AutoResizeColumnEX_Value = listView.Columns[listView.Columns.Count - 1].Width - orginalEndColumnWidth;

            listView.Columns[columnIndex].Width += AutoResizeColumnEX_Value;
            listView.Columns[listView.Columns.Count - 1].Width = orginalEndColumnWidth;
        }
        else
        {
            listView.Columns[columnIndex].Width -= AutoResizeColumnEX_Value;
            listView.Columns[listView.Columns.Count - 1].Width = orginalEndColumnWidth;
        }
    }
    else
    {
        listView.AutoResizeColumn(columnIndex, ColumnHeaderAutoResizeStyle.ColumnContent);
        listView.AutoResizeColumn(columnIndex, ColumnHeaderAutoResizeStyle.HeaderSize);
    }
}

I couldn't find a proper code so I decided to write it myself.
Here you can use it.

public static int AutoResizeColumnEX_Value = 0;

public static void AutoResizeColumnEX(this ListView listView, int columnIndex)
{
    if (columnIndex != (listView.Columns.Count - 1))
    {
        var orginalEndColumnWidth = listView.Columns[listView.Columns.Count - 1].Width;

        listView.AutoResizeColumn(listView.Columns.Count - 1, ColumnHeaderAutoResizeStyle.ColumnContent);
        listView.AutoResizeColumn(listView.Columns.Count - 1, ColumnHeaderAutoResizeStyle.HeaderSize);

        if (orginalEndColumnWidth < listView.Columns[listView.Columns.Count - 1].Width)
        {
            AutoResizeColumnEX_Value = listView.Columns[listView.Columns.Count - 1].Width - orginalEndColumnWidth;

            listView.Columns[columnIndex].Width += AutoResizeColumnEX_Value;
            listView.Columns[listView.Columns.Count - 1].Width = orginalEndColumnWidth;
        }
        else
        {
            listView.Columns[columnIndex].Width -= AutoResizeColumnEX_Value;
            listView.Columns[listView.Columns.Count - 1].Width = orginalEndColumnWidth;
        }
    }
    else
    {
        listView.AutoResizeColumn(columnIndex, ColumnHeaderAutoResizeStyle.ColumnContent);
        listView.AutoResizeColumn(columnIndex, ColumnHeaderAutoResizeStyle.HeaderSize);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文