50% OFF!!!

Thursday, August 21, 2008

Auto size ListView [columns]


Good feature for auto sizing the listview control.
this autosizes the columns of the list.
the auto size can be done with 2 ways:

1. adjust the width of the longest item in the column, set the Width property to -1
2. autosize to the width of the column heading, set the Width property to -2


Here is example for autosizing by the larges of the 2 options:



public static void AutoSizeListView(ListView p_listView)
{
int width1, width2;
ColumnHeader colHeader;
ListView.ColumnHeaderCollection colHeaderCollection = p_listView.Columns;

int count = colHeaderCollection.Count;
for (int i = 0; i < count; i++)
{
colHeader = colHeaderCollection[i];

// To adjust the width of the longest item in the column, set the Width property to -1
colHeader.Width = -1;
width1 = colHeader.Width;

// To autosize to the width of the column heading, set the Width property to -2
colHeader.Width = -2;
width2 = colHeader.Width;

colHeader.Width = (width1 <= width2) ? width2 : width1;
}
}





2 comments:

  1. Excellent! This works like a charm! Thanks!

    ReplyDelete
  2. Just what I was looking to do. My Thanks as well.

    ReplyDelete