【问题标题】:VB.net Change specific datagridview row color based on value in array [duplicate]VB.net根据数组中的值更改特定的datagridview行颜色[重复]
【发布时间】:2019-01-27 02:15:43
【问题描述】:

在我的表单上,我有一个 datagridview。我还有一个包含以下值的文本文件:

PersonName:PersonAge:ChampionTrueOrFalse

当调用 sub 时,文本文件中的所有值都会加载到数组中,第一个值(人名)会显示在 datagridview 中。 我想要实现的是:如果个人第四个值(ChampionTrueOrFalse)的值为真,则将该特定行的背景颜色为黄色。

我得到的最接近的是以下内容,但是当它应该只为列中具有“False”的单元格着色时,它会将所有单元格变为绿色。

   Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        For Each row1 As DataGridViewRow In DataGridView1.Rows

            If row1.Cells("ChampionTrueOrFalse").Value = "Yes" Then
                e.CellStyle.BackColor = Color.LightGreen
            End If

        Next
    End Sub

【问题讨论】:

  • 没有必要迭代该事件中的所有行。事件 args 将告诉您需要绘制哪一行。看看骗局

标签: vb.net datagridview


【解决方案1】:

没有理由不这样做。

If something Then DataGridView1.Rows(RowIndex).DefaultCellStyle.BackColor = Color.Yellow

【讨论】:

    【解决方案2】:

    终于搞定了!

    正确的代码是:

    For Each row1 As DataGridViewRow In DataGridView1.Rows
    
    If row1.Cells("ChampionTrueOrFalse").Value = "True" Then
                    row1.DefaultCellStyle.BackColor = Color.Gold
    End If
    
    Next
    

    【讨论】:

      猜你喜欢
      • 2018-05-12
      • 2021-11-27
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2022-01-19
      相关资源
      最近更新 更多