我有一个这样的列表视图:
显示项目描述和交付数量。
我已经给出了这样的代码,但它无效。
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
答案 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
时,请指定相应的索引值。