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.

2 comments:

  1. Thanks for posting this - was about to begin writing auto width detection code. I completely agree though, while the framework is meant to be slim, some left out things are just silly. No gridlines in listviews, no borders for panels, I could go on and on.

    More frustrating is the fact that the functionality is still there in the libs, as most of these can be achieved through pinvokes.

    ReplyDelete
  2. Oh, thank goodness! I can't believe that was it! Whew!!! THANK YOU!

    ReplyDelete