【发布时间】:2013-10-18 11:27:53
【问题描述】:
我想根据其状态更改我的 Listview 的颜色。 我有两种状态,我想将“待定”更改为红色,将“完成”更改为蓝色。 这怎么可能?我不知道,因为这是我第一次在列表视图中这样做。
【问题讨论】:
-
这意味着您应该在数据库中有一个名为 status 的列,其中数据 P 表示挂起,C 表示完成
标签: vb.net winforms listview colors row
我想根据其状态更改我的 Listview 的颜色。 我有两种状态,我想将“待定”更改为红色,将“完成”更改为蓝色。 这怎么可能?我不知道,因为这是我第一次在列表视图中这样做。
【问题讨论】:
标签: vb.net winforms listview colors row
可能是这样的:
Dim ListView1 As ListView = New ListView
ListView1.BackColor = if(status.tolower = "pending",Color.Red, Color.Blue)
或者您可以为单个项目着色:
Dim lvi As ListViewItem = New ListViewItem
lvi.Text = "Test"
lvi.BackColor = if(status.tolower = "pending",Color.Red, Color.Blue)
ListView1.Items.Add(lvi)
【讨论】:
IIf。这是一个垃圾VB函数。请改用有条件的If。
_backColor = If(status.tolower = "pending", Color.Red, Color.Blue)。我的观点是IIf 是一个糟糕的函数,它评估条件的两个部分,而不管(布尔)第一个参数如何评估。
If(status.tolower 中遇到错误,我该如何 ifx。我可以把那个代码放在哪里?在 Form_load 中?
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
【讨论】:
这就是我的做法,我将这些代码添加到我的 listview 循环事件中
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
【讨论】:
我在我的数据库中输入了一个数字类型,所以无论子项 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
【讨论】:
试试:
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
【讨论】:
.ForeColoe 只有 .ForeColor 。而且你不能在你的 with... 命令中再次使用.items(i).SubItems...。
UseItemStyleForSubItems 设置为 FALSE 很重要
It = New ListViewItem
It.UseItemStyleForSubItems = False
t.SubItems(It.SubItems.Count - 1).BackColor = Color.Red
【讨论】: