3

我想根据其状态更改我的 Listview 的颜色。我有两种状态,我想将“待定”更改为红色,将“完成”更改为蓝色。这怎么可能?我不知道,因为这是我第一次在列表视图中这样做。

4

6 回答 6

3

可能是这样的:

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)
于 2013-10-10T15:42:08.140 回答
1

尝试:

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
于 2017-01-19T09:07:13.773 回答
0
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
于 2014-07-18T01:46:11.847 回答
0

UseItemStyleForSubItems 设置 FALSE 很重要

It = New ListViewItem
It.UseItemStyleForSubItems = False
t.SubItems(It.SubItems.Count - 1).BackColor = Color.Red
于 2021-05-20T08:25:24.697 回答
0

这就是我的做法 我将这些代码添加到我的 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
于 2015-08-18T12:50:25.043 回答
0

我在我的数据库中输入了一个数字,所以无论子项 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
于 2016-07-17T14:40:45.990 回答