Tuesday 6 July 2010

ListView Column AutoResize method in .Net Compact Framework

If your a .Net developer using the Compact Framework like me, you've no doubt been frustrated by the lack of some of the most basic functionality. I know it's meant to have a small footprint but some of the things they opt to leave out bewiders me sometimes!

For example, the .Net CF Listview does not have the AutoResize method on a column. This can be frustrating as to do this manually would be pretty intensive and just un-necessary. There is a solution though, the functionality is still there, it's just hidden away.

If you set the width of the column to be -1, it will resize the column to be the width of the longest item in that column. If you set the width of the column to be -2, it will resize the column to the width of the column heading text.

This is exactly the same as using listView.Columns(0).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent) and listView.Columns(0).AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize) on the full .Net framework.