如何在加载时设置 DataGridView Rows FontStyle 的条件?

How to set DataGridView Rows FontStyle with condition at loading time?

如何根据条件更改DataGridView所有行的字体样式?

我的条件是if Cell Value Equal 0 then FontStyle = Strikeout otherwise Regular

Private Function DetailGridViewSetStyle()
    Dim dgv As DataGridView = DetailDataGridView
    Dim dgvInd As Integer = dgv.CurrentRow.Index
   
    For cc As Integer = 0 To dgv.ColumnCount - 1
        If dgv.Item(4, dgvInd).Value = 0 Then
            dgv.DefaultCellStyle.Font = New Font("ARIAL", 8, FontStyle.Strikeout)
        ElseIf dgv.Item(4, dgvInd).Value = 1 Then
            dgv.DefaultCellStyle.Font = New Font("ARIAL", 8, FontStyle.Regular)
        End If
    Next
End Function
  • 仅当您需要 return 某些内容时才使用“函数”,这不是您的情况,请改用 Sub。
  • dgv.DefaultCellStyle.Font 将为所有数据网格设置字体样式,您需要从指定行设置字体样式,然后使用 row.DefaultCellStyle.Font

如果我理解你的问题,这是你需要的代码:

Private Sub DetailGridViewSetStyle()
        Dim dgv As DataGridView = DetailDataGridView

        For Each row As DataGridViewRow In dgv.Rows
            Dim Value As Boolean = CBool(row.Cells(4).Value)

            If Value = False Then
                row.DefaultCellStyle.Font = New Font("ARIAL", 8, FontStyle.Strikeout)
            Else
                row.DefaultCellStyle.Font = New Font("ARIAL", 8, FontStyle.Regular)
            End If

        Next
    End Sub

如果您的 DataGridView 绑定到数据源,您可能希望在 DataBinding 完成后格式化网格的行。
订阅 DataBindingComplete 活动。

之后,您需要处理单元格值的变化,根据当前行中某列的值来格式化行。
订阅 CellValueChanged 活动。

我正在使用辅助方法来确定当前是什么,确定字体样式更改的单元格的值是多少,其 OwningRow 的当前字体是什么,然后将字体样式更改为指定的样式如果满足所有条件,否则恢复为默认字体样式。

注意:DataGridViewRow.DefaultCellStyle may be null, since the Row may inherit the Font from the DataGridView.DefaultCellStyle, so we need to check the DataGridViewRow.InheritedStyle 值。
在辅助方法中有一个使用合并表达式的赋值:

Dim rowFont = If(row.DefaultCellStyle.Font, row.InheritedStyle.Font)

如果此语法在您的 VB.Net 版本中不可用,请使用扩展形式:

 Dim rowFont = If(row.DefaultCellStyle.Font Is Nothing, row.InheritedStyle.Font, row.DefaultCellStyle.Font)

或者,如果当前单元格值为空 (Nothing) 或 DbNull.Value,则不执行任何操作:例如,如果 DataGridView.DataSource 设置为 DataTable,则可以。
如果您想将空单元格值解释为 0,请相应地更改代码。

Private Sub DetailDataGridView_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DetailDataGridView.CellValueChanged
    If e.ColumnIndex = 4 Then
        ToggleRowFontStyle(Of Integer)(DetailDataGridView(4, e.RowIndex), 0, FontStyle.Strikeout)
    End If
End Sub

Private Sub DetailDataGridView_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DetailDataGridView.DataBindingComplete
    For Each row As DataGridViewRow In DetailDataGridView.Rows
        ToggleRowFontStyle(Of Integer)(DetailDataGridView(4, row.Index), 0, FontStyle.Strikeout)
    Next
End Sub

辅助方法:

Private Sub ToggleRowFontStyle(Of T)(cell As DataGridViewCell, toggleValue As T, toggleFontStyle As FontStyle)
    If cell.Value Is Nothing OrElse cell.Value Is DBNull.Value Then Return

    Dim row As DataGridViewRow = cell.OwningRow
    Dim cellValue As T = CType(Convert.ChangeType(cell.Value, GetType(T)), T)
    Dim rowFont = If(row.DefaultCellStyle.Font, row.InheritedStyle.Font)

    If cellValue.Equals(toggleValue) Then
        If rowFont.Style <> toggleFontStyle Then
            rowFont = New Font(rowFont, toggleFontStyle)
        End If
    Else
        If rowFont.Style = toggleFontStyle Then
            rowFont = New Font(rowFont, FontStyle.Regular)
        End If
    End If
    row.DefaultCellStyle.Font = rowFont
End Sub