我有一个C#ListView,我想根据列的内容自动调整列宽。
我知道将列宽设置为-1会使列在最宽成员的长度上调整大小。 我知道将列宽设置为-2会使列的大小与列标题的长度相符。 如何将列的大小设置为两者中的较大者?
我可以这样做:
for (int i = 0; i < listView.Columns.Count; ++i) { listView.Columns[i].Width = -1; int width1 = listView.Columns[i].Width; listView.Columns[i].Width = -2; if (width1 > listView.Columns[i].Width) listView.Columns[i].Width = -1; }但它确实看起来非常低效。
有没有人有答案?
I have a C# ListView and I'd like to autosize the column widths based on the contents of the column.
I know that setting the column width to -1 will size the column at the length of the widest member. I know that setting the column width to -2 will size the column at the length of the column header. How do I size the column to be the greater of the two?
I could do something like this:
for (int i = 0; i < listView.Columns.Count; ++i) { listView.Columns[i].Width = -1; int width1 = listView.Columns[i].Width; listView.Columns[i].Width = -2; if (width1 > listView.Columns[i].Width) listView.Columns[i].Width = -1; }but it does seem fabulously inefficient.
Does anyone have an answer?
最满意答案
你应该试试
ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);即使它显示“ColumnHeaderAutoResizeStyle.HeaderSize”,它也应该自动调整大小到列中最大的项目,无论是标题还是列成员
You should try
ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);even though it says "ColumnHeaderAutoResizeStyle.HeaderSize" it should auto resize to the largest item in the column, whether it's the header or a column member
C#listview最佳列宽(C# listview optimum column width)我有一个C#ListView,我想根据列的内容自动调整列宽。
我知道将列宽设置为-1会使列在最宽成员的长度上调整大小。 我知道将列宽设置为-2会使列的大小与列标题的长度相符。 如何将列的大小设置为两者中的较大者?
我可以这样做:
for (int i = 0; i < listView.Columns.Count; ++i) { listView.Columns[i].Width = -1; int width1 = listView.Columns[i].Width; listView.Columns[i].Width = -2; if (width1 > listView.Columns[i].Width) listView.Columns[i].Width = -1; }但它确实看起来非常低效。
有没有人有答案?
I have a C# ListView and I'd like to autosize the column widths based on the contents of the column.
I know that setting the column width to -1 will size the column at the length of the widest member. I know that setting the column width to -2 will size the column at the length of the column header. How do I size the column to be the greater of the two?
I could do something like this:
for (int i = 0; i < listView.Columns.Count; ++i) { listView.Columns[i].Width = -1; int width1 = listView.Columns[i].Width; listView.Columns[i].Width = -2; if (width1 > listView.Columns[i].Width) listView.Columns[i].Width = -1; }but it does seem fabulously inefficient.
Does anyone have an answer?
最满意答案
你应该试试
ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);即使它显示“ColumnHeaderAutoResizeStyle.HeaderSize”,它也应该自动调整大小到列中最大的项目,无论是标题还是列成员
You should try
ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);even though it says "ColumnHeaderAutoResizeStyle.HeaderSize" it should auto resize to the largest item in the column, whether it's the header or a column member
发布评论