簡體   English   中英

如果字段中的文本為“否”,則更改datagridview中特定行的顏色

[英]change colour of specific row in datagridview if text in field says “No”

這是我的代碼:

For Each row As DataGridViewRow In DataGridView1.Rows
        Dim value = Val(row.Cells(9).Value)
        If value = "no" Then
            row.DefaultCellStyle.BackColor = Color.Red
        End If
    Next

而且我不斷收到此錯誤:

System.InvalidCastException: 'Conversion from string "no" to type 'Double' is not valid.'

有任何想法嗎?

提前致謝

不要使用Val因為這會將您的價值轉換為Double。 使用ToString將其轉換為字符串,然后將其與“ no”進行比較

For Each row As DataGridViewRow In DataGridView1.Rows
    If row IsNot Nothing _
        AndAlso row.Cells IsNot Nothing _
        AndAlso row.Cells(9) IsNot Nothing Then
        If If(row.Cells(9).Value, "").ToString().ToUpper() = "NO" Then
            row.DefaultCellStyle.BackColor = Color.Red
        ElseIf row.Cells(9).Value.ToString().ToUpper() = "YES" Then
            row.DefaultCellStyle.BackColor = Color.Green
        End If
    End If
Next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM