Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ListView Detail, Highlight a single cell

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.

I've experimented with Owner Drawn subitems, but using the below code, I get highlighted cells, but no text! Are there any ideas on how to get this working? Thanks for your help.

//m_PC.Location is the X,Y coordinates of the highlighted cell.


void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    if ((e.ItemIndex == m_PC.Location.Y) && (e.Item.SubItems.IndexOf(e.SubItem) == m_PC.Location.X))
        e.SubItem.BackColor = Color.Blue;
    else
        e.SubItem.BackColor = Color.White;
    e.DrawBackground();
    e.DrawText();
}
like image 538
Mike Christiansen Avatar asked Oct 18 '08 16:10

Mike Christiansen


3 Answers

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.

like image 142
Charlie Avatar answered Nov 20 '22 19:11

Charlie


Figured it out. Here's code to toggle the highlight of a specific subitem.

listView1.Items[1].UseItemStyleForSubItems = false;
if (listView1.Items[1].SubItems[10].BackColor == Color.DarkBlue)
{
    listView1.Items[1].SubItems[10].BackColor = Color.White;
    listView1.Items[1].SubItems[10].ForeColor = Color.Black;
}
else
{
    listView1.Items[1].SubItems[10].BackColor = Color.DarkBlue;
    listView1.Items[1].SubItems[10].ForeColor = Color.White;
}
like image 32
Mike Christiansen Avatar answered Nov 20 '22 20:11

Mike Christiansen


In my case, I wanted to highlight specific rows, including all the fields. So every row in my listview with "Medicare" in the first column gets the entire row highlighted:

public void HighLightListViewRows(ListView xLst)
        {
            for (int i = 0; i < xLst.Items.Count; i++)
            {
                if (xLst.Items[i].SubItems[0].Text.ToString() == "Medicare")
                {
                    for (int x = 0; x < xLst.Items[i].SubItems.Count; x++)
                    {
                        xLst.Items[i].SubItems[x].BackColor = Color.Yellow;
                    }
                }
            }
        }
like image 22
Chris Asquith Avatar answered Nov 20 '22 19:11

Chris Asquith