user2287884
user2287884

Reputation:

How can I fit column size to window size?

How can I fit the column range of my listview to the current window size. So that it will stretch in maximized window mode ?

Upvotes: 0

Views: 1017

Answers (1)

Cody Gray
Cody Gray

Reputation: 244752

Regardless of how you ask for it, the ListView supports only two ways of automatic column resizing: either by the length of the column content, or by the length of the column header content.

Since that's apparently not what you want, you will need to write the code to do this yourself. To do so, attach a handler method to your form's ResizeEnd event. That event gets raised whenever your form has been resized, either by the user or programmatically through code. Presumably, you've already used the Anchor and/or Dock properties on your ListView control to ensure that it resizes with its parent form, so this should cover all cases.

Inside of that event handler method, you will calculate the new sizes of each column and adjust the ListView control accordingly. This is the only hard part, figuring out which algorithm you want to use for column resizing.

If you have a ListView like this:

| Order # | Customer Name | Phone Number | Status |
|---------|---------------|--------------|--------|
|         |               |              |        |

You might decide that you want the "Order #" and "Status" columns to be both the same width and the narrowest (because they have the least amount of information to display). The "Customer Name" needs to be the longest (because it has the most amount of information to display), and the "Phone Number" can be somewhere in the middle.

So then all you have to do is size each column proportionally to the total available width of the ListView control, which you can retrieve by querying its ClientSize property; e.g.,

float totalWidth = myListView.ClientSize.Width;

You'll find a really elegant and reusable demonstration of this method here.

Upvotes: 1

Related Questions