问题
I want to change the color of my Listview according to its status. I have two status, the "PENDING" that I want to change in Red Color and "COMPLETE" to Blue. How is it possible? I have no Idea since this is my first time to do it in a list view.
回答1:
Something like this perhaps:
Dim ListView1 As ListView = New ListView
ListView1.BackColor = if(status.tolower = "pending",Color.Red, Color.Blue)
Or you can colour indiviual items:
Dim lvi As ListViewItem = New ListViewItem
lvi.Text = "Test"
lvi.BackColor = if(status.tolower = "pending",Color.Red, Color.Blue)
ListView1.Items.Add(lvi)
回答2:
Sub changeselectedItemcolour()
Try
'Get currently selected items index value
Dim i = ListView1.Items.Item(ListView1.SelectedIndices(0)).Index
Dim k As Integer = 0
'loop entire list and reset colors
While k <= ListView1.Items.Count - 1
ListView1.Items(k).BackColor = Color.FromArgb(255, 255, 255)
ListView1.Items(k).ForeColor = Color.Black
k = k + 1
End While
'set the selected items color
Try
ListView1.Items(i).BackColor = SystemColors.Highlight
ListView1.Items(i).ForeColor = Color.Red
Catch ex As Exception
End Try
Catch ex As Exception
End Try
end sub
回答3:
this is how i did mine i added these codes to my listview loop event
ListView1.Items.Clear()
Dim conn As SqlConnection
Dim recordinsert As SqlCommand
Dim searchme As SqlDataReader
Dim strQuery As String
conn = New SqlConnection(dbClass.Globconn)
conn.Open()
strQuery = "select * from salesdetail where invno= '" & txtProformaNo.Text & "' order by snno"
recordinsert = New SqlCommand(strQuery, conn)
searchme = recordinsert.ExecuteReader
Do While searchme.Read()
Lv = ListView1.Items.Add(searchme("snno"))
Lv.SubItems.Add(searchme("stkcode"))
Lv.SubItems.Add(searchme("stkdes"))
Lv.SubItems.Add(searchme("qty"))
Lv.SubItems.Add(searchme("sprice"))
Lv.SubItems.Add(searchme("amt"))
If searchme("status") = "S" Then
Lv.ForeColor = Color.Green
Else
Lv.ForeColor = Color.Red
End If
Loop
回答4:
i put in my database a type numbers so wherever the subitems 7 gives a number the column color will change according to what is saved in the database
For i As Integer = 0 To ListView1.Items.Count - 1
ListView1.Items(i).UseItemStyleForSubItems = False
If ListView1.Items(i).SubItems.Count > 1 Then
ListView1.Items(i).SubItems(1).BackColor = Color.AntiqueWhite
If ListView1.Items(i).SubItems(5).Text > 0 Then
ListView1.Items(i).SubItems(5).BackColor = Color.Red
End If
If ListView1.Items(i).SubItems(7).Text = 0 Then
ListView1.Items(i).SubItems(1).BackColor = Color.LightBlue
ListView1.Items(i).SubItems(1).ForeColor = Color.White
ElseIf ListView1.Items(i).SubItems(7).Text = 1 Then
ListView1.Items(i).SubItems(1).BackColor = Color.LightGray
ListView1.Items(i).SubItems(1).ForeColor = Color.White
ElseIf ListView1.Items(i).SubItems(7).Text = 2 Then
ListView1.Items(i).SubItems(1).BackColor = Color.LightSkyBlue
ListView1.Items(i).SubItems(1).ForeColor = Color.White
ElseIf ListView1.Items(i).SubItems(7).Text = 3 Then
ListView1.Items(i).SubItems(1).BackColor = Color.LightSteelBlue
ListView1.Items(i).SubItems(1).ForeColor = Color.White
ElseIf ListView1.Items(i).SubItems(7).Text = 4 Then
ListView1.Items(i).SubItems(1).BackColor = Color.AntiqueWhite
ElseIf ListView1.Items(i).SubItems(7).Text = 5 Then
ListView1.Items(i).SubItems(1).BackColor = Color.LightGreen
ListView1.Items(i).SubItems(1).ForeColor = Color.White
ElseIf ListView1.Items(i).SubItems(7).Text = 6 Then
ListView1.Items(i).SubItems(1).BackColor = Color.CadetBlue
ListView1.Items(i).SubItems(1).ForeColor = Color.White
ElseIf ListView1.Items(i).SubItems(7).Text = 7 Then
ListView1.Items(i).SubItems(1).BackColor = Color.LightYellow
ListView1.Items(i).SubItems(1).ForeColor = Color.Black
End If
End If
Next
回答5:
Try:
For i As Integer = 0 To ListView1.Items.Count - 1
With ListView1.Items(i)
.UseItemStyleForSubItems = False
If .Items(i).SubItems.Count > 1 Then
.Items(i).SubItems(0).ForeColoe = Color.Green
.Items(i).SubItems(1).BackColor = Color.Yellow
.Items(i).SubItems(2).BackColor = Color.Red
End If
End With
Next
来源:https://stackoverflow.com/questions/19300248/changing-color-of-a-row-of-listview-according-to-item