0

I have message is too long to fit in the column. I have table with four columns my fourth column is "Message" which has string long string and it doesnot fit in columnwidth. I want to Make the text in column as Warp so that all the text is visible to the user.

  ListViewItem lv = new ListViewItem();
                    lv.Text = det_view.filename;
                    lv.SubItems.Add(det_view.number.ToString());
                    lv.SubItems.Add(det_view.Date_Time.ToString());
                    lv.SubItems.Add(det_view.Message); // here the string too long and need wrap the message
                    listView1.Items.Add(lv);

Regards

User123
  • 373
  • 2
  • 7
  • 14

5 Answers5

2

Short of having your listviewitems ownerdrawn, you might have a look at ObjectListView. It will wordwrap just fine and may suite your needs.

itsmatt
  • 31,265
  • 10
  • 100
  • 164
2

If You feel some license problems with ObjectListView, you can use native .Net ListView.

It can also wordwrap in view=Details and setting smallImageList

(with picture height = 32 or more).

Example of native ListView

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
DimDim
  • 148
  • 1
  • 10
  • But How ? I am using VS 2008 and Windows CE application. Is it possible to achieve this ? – kbvishnu Aug 13 '13 at 13:24
  • This does seem to work, thought every row is the same height. Also some cells don't seem to wrap but get one line cut off with an ellipsis. – Tom Winter May 19 '15 at 20:09
1

You may try Better ListView component, which supports multi-line items with varous text wrapping and trimming methods:

enter image description here

Libor
  • 3,285
  • 1
  • 33
  • 41
1

Here is a class inheriting from ListView that will grow your row height to fit the text in a column. I believe the default will not word break. So you would need to implement wordbreak if that's something you want.

class WordWrapListView : ListView
{
    private const int LVM_FIRST = 0x1000;
    private const int LVM_INSERTITEMA = (WordWrapListView.LVM_FIRST + 7);
    private const int LVM_INSERTITEMW = (WordWrapListView.LVM_FIRST + 77);

    private Graphics graphics;

    public WordWrapListView()
    {
        this.graphics = this.CreateGraphics();
        base.View = View.Details;

        this.AutoSizeRowHeight = true;

    }

    //overriding WndProc because there are no item added events
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            // Detect item insert and adjust the row size if necessary based on the text
            // add in LVM_DELETEITEM and LVM_DELETEALLITEMS and reset this.rowHeight if you want to reduce the row height on remove
            case WordWrapListView.LVM_INSERTITEMA:
            case WordWrapListView.LVM_INSERTITEMW:
                {
                    ListViewItem lvi = this.Items[this.Items.Count - 1];

                    for (int i = 0; i< lvi.SubItems.Count; ++i)
                    {
                        ListViewItem.ListViewSubItem lvsi = lvi.SubItems[i];

                        string text = lvsi.Text;

                        int tmpHeight = 0;
                        int maxWidth = this.Columns[i].Width;

                        SizeF stringSize = this.graphics.MeasureString(text, this.Font);

                        if (stringSize.Width > 0)
                        {
                            tmpHeight = (int)Math.Ceiling((stringSize.Width / maxWidth) * stringSize.Height);

                            if (tmpHeight > this.rowHeight)
                            {
                                this.RowHeight = tmpHeight;
                            }
                        }
                    }
                }
                break;

            default:
                break;
        }
        base.WndProc(ref m);
    }

    private void updateRowHeight()
    {
        //small image list hack
        ImageList imgList = new ImageList();
        imgList.ImageSize = new Size(this.rowHeight, this.rowHeight);
        this.SmallImageList = imgList;
    }

    [System.ComponentModel.DefaultValue(true)]
    public bool AutoSizeRowHeight { get; set; }

    private int rowHeight;
    public int RowHeight 
    {
        get
        {
            return this.rowHeight;
        }
        private set
        {
            //Remove value > this.rowHeight if you ever want to scale down the height on remove item
            if (value > this.rowHeight && this.AutoSizeRowHeight)
            {
                this.rowHeight = value;
                this.updateRowHeight();
            }
        }
    }

    // only allow details view
    [Browsable(false), Bindable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
    public new View View
    {
        get
        {
            return base.View;
        }
        set
        {

        }
    }

}
  • Your code is helpful, just i d like to have a precision about LVM_FIRST, why 0x1000 value ? and also in updateRowHeight, i don't really understand the hack, can you explain me please ? – Pierre-Olivier Pignon Nov 30 '16 at 07:52
  • 1
    By overriding WndProc we are basically intercepting the Windows message loop so that we can respond to events not explicitly defined by the listview. The definitions for LVM_* come from Commctrl.h; they are Windows constants. The actual ability to update the row height came from: http://stackoverflow.com/questions/6563863/c-sharp-change-listview-items-rows-height that is unfortunately all I know about the "hack". – curlyhairedgenius Jan 12 '17 at 00:34
1

May not be what you need but my solution was to switch to the build-in DataGridView control.

Tom Winter
  • 1,813
  • 3
  • 18
  • 23