静的な幅の列幅が計算された後、残りのサイズに基づいて ListView の特定の列のみのサイズを変更しようとしています。たとえば、数量、価格などの列のサイズを変更したくはありませんが、名前や説明など、通常は幅の広いデータを含む列の幅を広げたいと考えています。これは私が以下で使用しようとしている方法ですが、機能していません。
ところで、ListView で ClientSizeChanged イベントが発生したときに、このメソッドを呼び出しています。それが関連しているかどうかはわかりません。
public static void ResizeListViewColumns(ListView lv, List<int> fixedColumnIndexes, List<int> nonFixedColumnIndexes)
{
int lvFixedWidth = 0;
int lvNonFixedWidth = 0;
if (fixedColumnIndexes.Count + nonFixedColumnIndexes.Count != lv.Columns.Count)
{
throw new Exception("Number of columns to resize does not equal number of columns in ListView");
}
else
{
// Calculate the fixed column width
// Calculate the non-fixed column width
// Calculate the new width of non-fixed columns by dividing the non-fixed column width by number of non-fixed columns
foreach (var fixedColumn in fixedColumnIndexes)
{
lvFixedWidth += lv.Columns[fixedColumn].Width;
}
foreach (var nonFixedColumn in nonFixedColumnIndexes)
{
lvNonFixedWidth += lv.Columns[nonFixedColumn].Width;
}
int numNonFixedColumns = nonFixedColumnIndexes.Count;
foreach (var newNonFixedColumn in nonFixedColumnIndexes)
{
lv.Columns[newNonFixedColumn].Width = lvNonFixedWidth / numNonFixedColumns;
}
}
}