C# listview 不同内容的字,颜色不同(急)

2025-06-27 09:50:46
推荐回答(3个)
回答1:

foreach (ListViewItem lvItems in myListView.Items)
{
if (lvItems.SubItems[0].Text == "正常")
{
//lvItems.BackColor = Color.Black;//背景色
lvItems.ForeColor = Color.Black;//字体颜色
}
else// if (lvItems.SubItems[0].Text == "异常")
{
//lvItems.BackColor = Color.Red;//背景色
lvItems.ForeColor = Color.Red;//字体颜色
}
}

回答2:

foreach (ListViewItem item in lsvStatus.Items)//对单元格进行字体或字颜色设置
{
item.UseItemStyleForSubItems = false;
if (item.SubItems[2].Text == "1") item.SubItems[2].ForeColor = Color.Red;
else if (item.SubItems[2].Text == "2") item.SubItems[2].ForeColor = Color.Yellow;
else if (item.SubItems[2].Text == "3") item.SubItems[2].ForeColor = Color.Blue;
else item.SubItems[2].ForeColor = Color.Green;
}
只有设置了ListViewItem 的 UseItemStyleForSubItems 为False。那么ListViewItem.ListViewSubItem的样式设置才有效。

回答3:

这个以前做过,你得先判断内容,在设置litview.rows[i].背景色