ListView動態修改列寬

在DataGridView中有autocolumnmode來動態改變現實Column的列寬,在ListView中是沒有這個屬性的,可以通過ListView.SizeChanged 事件去修改列寬,實現相同的效果
代碼如下

private bool Resizing = false;
 
private void ListView_SizeChanged(object sender, EventArgs e)
{
    // Don't allow overlapping of SizeChanged calls
    if (!Resizing)
    {
        // Set the resizing flag
        Resizing = true;
 
        ListView listView = sender as ListView;
        if (listView != null)
        {                               
            float totalColumnWidth = 0;
 
            // Get the sum of all column tags
            for (int i = 0; i < listView.Columns.Count; i++)
                totalColumnWidth += Convert.ToInt32(listView.Columns[i].Tag);
 
            // Calculate the percentage of space each column should 
            // occupy in reference to the other columns and then set the 
            // width of the column to that percentage of the visible space.
            for (int i = 0; i < listView.Columns.Count; i++)
            {
                float colPercentage = (Convert.ToInt32(listView.Columns[i].Tag) / totalColumnWidth);
                listView.Columns[i].Width = (int)(colPercentage * listView.ClientRectangle.Width);             
            }
        }
    }
 
    // Clear the resizing flag
    Resizing = false;
}

參考鏈接:


C#: ListView – Dynamically Sizing Columns to Fill Whole Control

http://nickstips.wordpress.com/2010/11/10/c-listview-dynamically-sizing-columns-to-fill-whole-control/



——————————————————————————————————————————————————————————————————

歡迎大神光臨菜鳥博客,希望能得到各位大神在編碼方面的指引,同時歡迎與我一樣剛進入編程世界的朋友一起討論學習!相信前進的道路上,有你們,編程世界會更加精彩!


發佈了41 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章