C#ListView详细信息,突出显示单个单元格

我在C#中使用ListView来制作网格。 我想找到一种能够以编程方式突出显示特定单元格的方法。 我只需要突出显示一个单元格。

我已经尝试了所有者绘制的子项目,但使用下面的代码,我得到突出显示的单元格,但没有文字! 有没有关于如何使这个工作的想法? 谢谢你的帮助。

//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(); } 

您可以在没有所有者的情况下绘制列表:

 // 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 ) ); 

ListViewSubItem构造函数的Color参数控制子项的前景色和背景色。 这里要做的关键是在列表项UseItemStyleForSubItems设置为False,否则将忽略您的颜色更改。

我认为你的所有者绘制解决方案也会有效,但你必须记住在将背景更改为蓝色时更改文本(前景)颜色,否则文本将很难看到。

弄清楚了。 这是用于切换特定子项的突出显示的代码。

 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; } 

就我而言,我想突出显示特定的行,包括所有字段。 因此,在第一列中使用“Medicare”的listview中的每一行都会突出显示整行:

 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; } } } }