Variable height rows in WinForms ListView

19,661

Solution 1

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.

Solution 2

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".

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
19,661
dbkk
Author by

dbkk

Updated on August 08, 2022

Comments

  • dbkk
    dbkk over 1 year

    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.

  • dbkk
    dbkk about 15 years
    I tried the GridView, but for some reason it is much slower (in VirtualMode with many items).
  • dbkk
    dbkk about 15 years
    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.
  • Mr. Bhosale
    Mr. Bhosale about 15 years
    If it isn't possible to use the DataGridView, then this seems like your only option.
  • dbkk
    dbkk about 15 years
    This is for ListBox, not ListView. A different control, that doesn't support MVC / virtual mode.
  • Chris Thompson
    Chris Thompson about 15 years
    He didn't specify that he needed virtual mode, so I was just suggesting an alternate control that would meet the needs.