添加颜色,具体取决于列表视图中的项目值

时间:2016-09-01 04:29:31

标签: vb.net winforms listview

我有一个这样的列表视图:

First part

Second part

显示项目描述和交付数量。

  • 如果交付了所有数量,我需要以绿色显示行。
  • 如果有任何数量交付(部分),那么我需要以橙色显示。
  • 如果没有交付数量,我需要显示黄色。

我已经给出了这样的代码,但它无效。

ListView1.Font = New System.Drawing.Font("Tahoma", 8.0!, System.Drawing.FontStyle.Bold)
ListView1.Items.Clear()
If ds.Tables(0).Rows.Count > 0 Then
    Dim dt As DataTable = ds.Tables(0)
    Dim str(ds.Tables(0).Columns.Count) As String
    Dim lvi As ListViewItem
    Dim rr As DataRow
    Dim highlight As String = ""
    Dim temp1 As String = ""
    Dim temp2 As String = ""
    Dim temp1Sum, temp2Sum As Integer
    Dim diffCount As Integer = 0
    Dim newColumn As Integer = 0
    Dim strTemp As String
    For Each rr In dt.Rows
        For col As Integer = 0 To ds.Tables(0).Columns.Count - 1
            str(col) = rr(col).ToString()
            If col > 4 And col > newColumn Then
                Dim qtyVal As String
                qtyVal = rr(col).ToString
                strTemp = qtyVal

                If temp1 = "" Then
                    temp1 = IIf(qtyVal = "", "0.00", qtyVal)
                Else
                    temp2 = temp1
                    temp1 = IIf(qtyVal = "", "0.00", qtyVal)
                    newColumn = col + 1
                End If
                If temp1 <> "" And temp2 <> "" Then
                    If temp1 <> temp2 Then
                        diffCount = diffCount + 1
                    End If

                    temp1 = ""
                    temp2 = ""
                End If
            End If

        Next
    lvi = New ListViewItem(str)
    ListView1.Items.Add(lvi)
    If diffCount = 0 Then
        lvi.BackColor = Color.Green

        noofdeliver = noofdeliver + 1
        txtdelivercount.Text = noofdeliver
    ElseIf diffCount > 0 Then
        If temp2Sum = 0 Then
            lvi.BackColor = Color.Yellow
        Else
            lvi.BackColor = Color.Orange

        End If
    End If
    temp2Sum = 0
        diffCount = 0
        newColumn = 0
    Next
End If

2 个答案:

答案 0 :(得分:0)

在变色后放置此行ListView1.Items.Add(lvi)

测试代码

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim lvi = New ListViewItem("foo " & ListView1.Items.Count + 1)
    lvi.BackColor = Color.Red
    ListView1.Items.Add(lvi)

End Sub

答案 1 :(得分:0)

这里的实际问题是...... 您使用str变量作为数组,但是您将ListViewItem指定为lvi = New ListViewItem(str)而未指定str数组的索引。正如您在代码中指定的那样,它始终只接受数组的第一个值。

解决方案:当您将数组str值分配给ListViewItemlvi时,请指定相应的索引值。