【问题标题】:adding color, depending on item value in list view添加颜色,取决于列表视图中的项目值
【发布时间】:2016-09-01 04:29:30
【问题描述】:

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

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

  • 如果所有数量都交付了,我需要用绿色显示线。
  • 如果有任何数量交付(部分)然后,我需要显示为橙色。
  • 如果当时没有发货,我需要显示黄色。

我给出了这样的代码,但它不起作用。

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

【问题讨论】:

  • 有什么更新吗?任何人都可以提供任何帮助

标签: vb.net winforms listview


【解决方案1】:

换色后加上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

【讨论】:

  • 那有什么用?
  • 当您执行ListView1.Items.Add() 时,对真实对象的引用将丢失。我添加了一个测试代码
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多