根据项目更改一行listview的颜色

时间:2013-10-10 15:36:52

标签: vb.net winforms listview colors row

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

6 个答案:

答案 0 :(得分:2)

或许这样的事情:

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)

答案 1 :(得分: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

结束子

答案 2 :(得分: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

答案 3 :(得分: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

答案 4 :(得分:0)

尝试:

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

答案 5 :(得分:0)

UseItemStyleForSubItems 设置 FALSE 非常重要

It = New ListViewItem
It.UseItemStyleForSubItems = False
t.SubItems(It.SubItems.Count - 1).BackColor = Color.Red