C# ListView Detail, Highlight a single cell

后端 未结 3 1736
既然无缘
既然无缘 2021-01-02 04:45

I\'m using a ListView in C# to make a grid. I would like to find out a way to be able to highlight a specific cell, programatically. I only need to highlight one cell.

3条回答
  •  一整个雨季
    2021-01-02 05:26

    You can do this without owner-drawing the list:

    // create a new list item with a subitem that has white text on a blue background
    ListViewItem lvi = new ListViewItem( "item text" );
    lvi.UseItemStyleForSubItems = false;
    lvi.SubItems.Add( new ListViewItem.ListViewSubItem( lvi,
        "subitem", Color.White, Color.Blue, lvi.Font ) );
    

    The Color arguments to the ListViewSubItem constructor are controlling the foreground and background color of the subitem. The critical thing to do here is set UseItemStyleForSubItems to False on the list item, otherwise your color changes will be ignored.

    I think your owner-draw solution would have worked as well, but you have to remember to change the text (foreground) color when you change the background to blue, otherwise the text will be hard to see.

提交回复
热议问题