代码之家  ›  专栏  ›  技术社区  ›  KJSR

DataGridView行前景色未更改

  •  0
  • KJSR  · 技术社区  · 6 年前

    我有一个方法,可以在我的 DataGridView ForeColor 数据表格控件

    Private Shared Sub dgvRowFormatting(dgv As DataGridView) 
        For Each row As DataGridViewRow In dgv.Rows            
            row.DefaultCellStyle.ForeColor = lColor
        Next
    End Sub
    

    一旦我指定了,我就通过了这个方法 BindingSource DataGridView DataSource 详情如下:

    bindingSource.DataSource = customerList
    dgv.DataSource = bindingSource
    dgvRowFormatting(dgv)
    

    我不确定我是否遗漏了什么?

    3 回复  |  直到 6 年前
        1
  •  1
  •   TnTinMn    6 年前

    迭代单元格或行并基于条件设置样式属性是不可取的,因为这会浪费资源,并且每次更改单个值时都必须调用样式设置方法以确保正确的样式设置。

    CellFormatting Event 以执行此自定义。

    为了演示,下面是一个简单的示例,它根据偶数/奇数行索引有条件地设置前景色。

    Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        If (e.RowIndex And 1) = 1 Then
            e.CellStyle.ForeColor = Color.Red ' odd numbered row
        Else
            e.CellStyle.ForeColor = Color.Black ' even numbered rows
        End If
    End Sub
    

    DataGridView.AlternatingRowsDefaultCellStyle Property 如果条件只是交替行。

    有关更多指导,请参阅: Best Practices for Scaling the Windows Forms DataGridView Control .

        2
  •  1
  •   Jimi    6 年前

    您只需要修改 RowsDefaultCellStyle ,在设置 DataSource
    不需要设置 DefaultCellStyle 每行:

    dgv.DataSource = bindingSource
    dgv.RowsDefaultCellStyle.ForeColor = lColor
    

    交替 行,使用 AlternatingRowsDefaultCellStyle

    dgv.DataSource = bindingSource
    dgv.AlternatingRowsDefaultCellStyle.ForeColor = lColor
    

    如果要将所有行的单元格更改为其他单元格 ForeColor 根据某些条件,您需要在某个地方指定这些条件。

    颜色属性的条件设置器示例
    用于更改 前景色 Cells Row 根据一些预定义的条件设置为不同的值:a ([Column], [Row]).Value

    Private Shared Sub dgvRowFormatting(dgv As DataGridView)
        For Each row As DataGridViewRow In dgv.Rows
            Dim CompareValue = Convert.ToInt32(dgv(1, row.Index).Value)
            row.DefaultCellStyle.ForeColor = MyConditions.SetCondition(CompareValue)
        Next
    End Sub
    
    Public Class MyConditions
        Public Enum Condition
            LessThanZero = -1
            EqualToZero
            GreaterThanZero
        End Enum
    
        Public Shared Function SetCondition(Of T As IComparable)(ByVal Value As T) As Color
            Return SetColorOnCondition(CType(Value.CompareTo(0), Condition))
        End Function
    
        Private Shared Function SetColorOnCondition(Comparison As Condition) As Color
            Select Case Comparison
                Case Condition.LessThanZero
                    Return Color.Red
                Case Condition.EqualToZero
                    Return Color.Black
                Case Condition.GreaterThanZero
                    Return Color.Green
            End Select
        End Function
    End Class
    
        3
  •  0
  •   Jortx    6 年前

    当然,你可以用其他的方式来做这些事情(参见@Jimi's或@TnTinMn's的帖子)。

    但是你的代码运行良好。唯一的办法就是改变现状 颜色

    在运行它之前,只需确保在datagridview中填充了一些数据。

    您应该在gridview中看到红色文本。

    Private Shared Sub dgvRowFormatting(dgv As DataGridView)
        For Each row As DataGridViewRow In dgv.Rows
            row.DefaultCellStyle.ForeColor = Color.Red
        Next
    End Sub
    

    这里有一个基于列ID值更改颜色的代码示例:

    Private Shared Sub dgvRowFormatting(dgv As DataGridView)
        For Each row As DataGridViewRow In dgv.Rows
            Select Case row.Cells("Id").Value
                Case 1, 2
                    row.DefaultCellStyle.ForeColor = Color.Red
                Case 3, 4
                    row.DefaultCellStyle.ForeColor = Color.Blue
                Case Else
                    row.DefaultCellStyle.ForeColor = Color.Green
            End Select
        Next
    End Sub