Search code examples
c#listviewcompact-frameworkborderlistviewitem

ListViewItem Border - Compact Framework


I have a ListView where I add as Items some ListViewItems. The View property is set to Details. When the ListView is displayed the ListViewItems haven't any border (top and bottom line that separates one item from another).

How can I do to add a border to all my items?

An example:

enter image description here


Solution

  • For whatever reason, Gridlines aren't supported by the CF control, though the underlying native ListView does. P/Invoke to the rescue.

    private const uint LVM_FIRST = 0x1000;
    private const uint LVM_SETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 54;
    private const uint LVM_GETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 55;
    private const uint LVS_EX_GRIDLINES = 0x00000001;
    
    [DllImport("coredll.dll")]
    private static extern uint SendMessage(IntPtr hwnd, uint msg, uint wparam, uint lparam);
    
    public void EnableGridlines(ListView listView)
    {
        var style = SendMessage(
                listView.Handle,
                LVM_GETEXTENDEDLISTVIEWSTYLE,
                0,
                0);
    
        style |= LVS_EX_GRIDLINES;
    
        var style = SendMessage(
                listView.Handle,
                LVM_SETEXTENDEDLISTVIEWSTYLE,
                0,
                style);    
    }