vb.net 根据项目更改一行列表视图的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19300248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing color of a row of listview according to item
提问by BartSabayton
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.
我想根据其状态更改我的 Listview 的颜色。我有两个状态,“待定”,我想将其更改为红色,将“完成”更改为蓝色。这怎么可能?我不知道,因为这是我第一次在列表视图中执行此操作。
回答by twoleggedhorse
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)
回答by coski
Sub changeselectedItemcolour()
子更改selectedItemcolour()
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
结束子
回答by kabifarm
this is how i did mine i added these codes to my listview loop event
这就是我的方式我将这些代码添加到我的列表视图循环事件中
ListView1.Items.Clear()
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
回答by abdulrahman rabeie
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
我在我的数据库中输入了一个类型数字,所以无论子项 7 给出一个数字,列颜色都会根据数据库中保存的内容而改变
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
回答by Shah
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