如何根据项目值更改列表视图项目的单元格(背面或前景色)的颜色



如何根据带有if条件的项目值更改列表视图项目的单元格(背面或前景色(的颜色。

我这样做了,但它不起作用(意味着不改变颜色但显示按摩(并且没有显示错误,调试以逐行单步执行代码并检查控件和变量的值 请帮帮我...

Private Sub btnaddcat_Click(ByVal sender As System.Object, ByVal e As For k = 0 To ListView1.Items.Count - 1
If ListView1.Items(k).SubItems(6).Text > 100 Then
ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
MsgBox("hi test code yes")
Else
MsgBox("hi test code no")
End If
Next k
End Sub

我认为你在ItemDataBound事件中已经做到了。 它将在列表视图中的数据绑定期间自动为每一行执行代码。

例:

Private Sub listViewName_DataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles listViewName.ItemDataBound
'Add your condition for change color here
If e.Items.SubItems(6).Text > 100 Then
e.Items.SubItems(6).ForeColor = Color.Red
Else
e.Items.SubItems(6).ForeColor = Color.Blue
End If
End Sub

最新更新