ListView Question...

G

Guest

I have a listview control containing a number of columns.
When I populate the listview columns with data I want to size each column
according to the following rules:

1. If the width of the data is greater than the width of the column header's
title then size the column to be the width of the data value.

2. If the width of the column header's title is greater than the width of
the data in the column then size the column to be the width of the column
header's title.

I can use the ColumnHeader.AutoResize method to size on either width of data
value or column header title, BUT, I cannot seem to be able to determine
which is the wider of the two to avoid displaying the ellipse (...). I was
trying to look for a way of detecting the ellipse (...) in the column
header's title but to no avail.

Does anyone out there know a way to do this?
Any assistance would be very much appreciated.
Many thanks.
 
S

Stoitcho Goutsev \(100\)

Steve,

comparing the length of the strings won't do obviously because the size of
the text on the screen and number of latters are not related e.g. 'WW' will
be wider than 'iii'.

I can see only 2 solutions:
1. Is to measure the displayed suze of the column header and content using
Graphics.MeasureString or TextRenderer.MeasureText
or

2. Depending on your expectations for the widest string you resize according
it and hope for the best. For exaple. The column header says ID and the
column value is a number at up to 4 digits long. It could be that all
columns have 1 digit number, but if you expect tha most of the times the ID
are going to be 4 digits it make sense to resize based on the column content
rather than the header.

I personally would preffer the 2 solution.
 
J

Jason Newell

foreach (ColumnHeader ch in this.listView1.Columns)
{
ch.Width = -2;
}

Jason Newell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top