gpt4 book ai didi

vb.net - 更改 ListView 子项的前景色

转载 作者:行者123 更新时间:2023-12-04 17:35:35 24 4
gpt4 key购买 nike

我正在尝试根据 ListView 子项中的值更改某些列的前景色。我尝试了各种选择并查看了关于 SO 的各种帖子,但似乎没有任何效果。

在我目前的代码中,不是更改 dr(9),而是更改 dr(0)。我哪里错了。谢谢

Using dr = oledbCmd.ExecuteReader()


'clear items in the list before populating with new values
'lvRequests.Items.Clear()

While dr.Read()
If dr.HasRows Then
Dim LVI As New ListViewItem

With LVI

.UseItemStyleForSubItems = False
.Text = dr(0).ToString()
.SubItems.Add(CDate(dr(5)).ToShortDateString())
.SubItems.Add(dr(1).ToString())
.SubItems.Add(dr(3).ToString())

If dr(3).ToString = "D" Then
.SubItems(3).Text = "Destroyed"

ElseIf dr(3).ToString = "O" Then
.SubItems(3).Text = "Out"

ElseIf dr(3).ToString = "I" Then
.SubItems(3).Text = "Intake"
End If

.SubItems.Add(dr(9).ToString())

If IsDBNull(dr(9)) Then
.SubItems(LVI.SubItems.Count - 1).Text = "O/S"
.ForeColor = Color.DarkRed


ElseIf dr(9) IsNot "DEMO" Then
.SubItems(LVI.SubItems.Count - 1).Text = "Done"

End If

End With
lvRequests.Items.Add(LVI)

lvcount += 1

End If

End While
End Using

最佳答案

您必须设置 UseItemStyleForSubItems每个人的属性(property)ListViewItem为假。

For i As Integer = 0 To (Me.ListView1.Items.Count - 1)
Me.ListView1.Items(i).UseItemStyleForSubItems = False
Next

编辑

我看到您指的是一个不存在的子项:
.Text = dr(0).ToString()
.SubItems.Add(CDate(dr(5)).ToShortDateString()) '< Index: 0
.SubItems.Add(dr(1).ToString()) '< Index: 1
.SubItems.Add(dr(3).ToString()) '< Index: 2

If dr(3).ToString = "D" Then
.SubItems(3).Text = "Destroyed" '<- No subitems with index 3 exists!

尝试将代码更改为:
Dim list As New List(Of ListViewItem)
Dim item As ListViewItem = Nothing
Dim subItems As ListViewItem.ListViewSubItem() = Nothing

Using dr = oledbCmd.ExecuteReader()

While dr.Read()

item = New ListViewItem()
item.UseItemStyleForSubItems = False
item.Text = dr(0).ToString()

subItems = New ListViewItem.ListViewSubItem(3 - 1) {New ListViewItem.ListViewSubItem(), New ListViewItem.ListViewSubItem(), New ListViewItem.ListViewSubItem()}
subItems(0).Text = CDate(dr(5)).ToShortDateString()
subItems(1).Text = dr(1).ToString()

Select Case dr(3).ToString
Case "D" : subItems(2).Text = "Destroyed"
Case "O" : subItems(2).Text = "Out"
Case "I" : subItems(2).Text = "Intake"
Case Else : subItems(2).Text = ""
End Select

If IsDBNull(dr(9)) Then
subItems(3).Text = "O/S"
subItems(3).ForeColor = Color.DarkRed
ElseIf dr(9) IsNot "DEMO" Then
subItems(3).Text = "Done"
Else
subItems(3).Text = dr(9).ToString()
End If

item.SubItems.AddRange(subItems)
list.Add(item)

End While

End Using

Me.lvRequests.Items.AddRange(list.ToArray())

关于vb.net - 更改 ListView 子项的前景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20593025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com