Variable height rows in WinForms ListView
Asked Answered
C

7

7

Is it possible to have variable-height rows within a WinForms ListView in Details mode?

There is no RowHeight or Rows[i].Height property on the control as far as I know.

Some blogs suggests implementing OwnerDraw, which I did, but I still can't find anything resembling height property from within the event handlers.

Ideally, the row height would auto-size to show multiline text when needed.

Considerable answered 9/3, 2009 at 12:33 Comment(0)
C
2

One option to think of:

To override the item height of all rows, create a dummy ImageList and set it to the desired height and assign it to the listview depending on the view or grasp the concept of http://www.codeproject.com/KB/list/changerowheight.aspx

If you use ObjectListView, you can change the item height of all rows pretty easily. But still, there is no way to change the individual item height as long as you are using ListView.

Chiliad answered 9/3, 2009 at 12:56 Comment(1)
That is good as a one-off solution (if height is known already), but I'd like it to adjust to long multi-line cells.Considerable
E
4

If you are using details mode, I wonder if DataGridView wouldn't be more versatile. Each row has a Height, or you can use AutoSizeRowsMode to do it automatically. See MSDN "Resizing Columns and Rows in the Windows Forms DataGridView Control".

Editorial answered 9/3, 2009 at 12:54 Comment(1)
I tried the GridView, but for some reason it is much slower (in VirtualMode with many items).Considerable
C
2

One option to think of:

To override the item height of all rows, create a dummy ImageList and set it to the desired height and assign it to the listview depending on the view or grasp the concept of http://www.codeproject.com/KB/list/changerowheight.aspx

If you use ObjectListView, you can change the item height of all rows pretty easily. But still, there is no way to change the individual item height as long as you are using ListView.

Chiliad answered 9/3, 2009 at 12:56 Comment(1)
That is good as a one-off solution (if height is known already), but I'd like it to adjust to long multi-line cells.Considerable
S
2

The ObjectListView mentioned in the first answer does not support variable row heights. It says this clearly in its FAQ. The underlying Windows listview control simply does not support variable row height. You have to look to other, custom written controls.

You may want to consider Matthew Hall’s excellent XPTable and its update project, as well as Lee Paul Alexander’s fantastic Outlook-style list.

Sinkhole answered 13/5, 2009 at 6:42 Comment(0)
A
1

The ListBox control does support variable height rows, but you have do all the drawing yourself.

Set the DrawMode to OwnerDrawVariable

Then add

protected override void OnDrawItem(DrawItemEventArgs e)
{
  /* Drawing code here */
}

protected override void OnMeasureItem(MeasureItemEventArgs e)
{
  /* Measure code here */
}

I use an owner-drawn listbox in a program called Task Reporter to list each task the user entered. Each entry is differently depending on how much text is entered.

Amphiboly answered 13/3, 2009 at 0:13 Comment(3)
If it isn't possible to use the DataGridView, then this seems like your only option.Iridis
This is for ListBox, not ListView. A different control, that doesn't support MVC / virtual mode.Considerable
He didn't specify that he needed virtual mode, so I was just suggesting an alternate control that would meet the needs.Amphiboly
U
1

If you are OK with 3rd party components, Better ListView supports exactly this (each item has a CustomHeight property so each can have different height):

enter image description here

Unbridle answered 22/4, 2014 at 23:22 Comment(0)
H
0

The Windows ListView control itself (which is wrapped by the .NET ListView control) doesn't support variable row heights.

If you stick with the ListView, you'll need to have fixed height rows.

Hobbyhorse answered 11/3, 2009 at 21:44 Comment(0)
D
0

If variable height rows are what you want, I'd consider using the DataGridView instead. It very much supports variable height rows (through the use of cell styles) and is much easier to use than trying to shoehorn the list view into doing what you want.

Dight answered 13/3, 2009 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.