C#ListView细节,突出显示一个单元格
问题描述:
我在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所控制的子项的前景色和背景色。此处要做的关键事项是在列表项上将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”中的每一行都会突出显示整行:
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;
}
}
}
}
Winforms or web? – tvanfosson 2008-10-18 17:02:38