vb.netwinformsif-statementdatagridviewfont-style

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


How to change the Font Style of all Rows of a DataGridView based on a conditions?

My condition is 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

Solution

  • If I understood your question, here's the code that you need:

    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