Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a datagridview in my project, i wish to strike a particular data if one of the cell in the data reads "Void" but when i did with the code i used all data in the datagridview was strike meaning accepting the first argument to all data despite some of the data in the said cells reads "Active"

What I have tried:

For Each r As DataGridViewRow In frmCheckOut_Room.DataGridView2.Rows
                        If (r.Cells(9).Value) = "Void" Then
                            r.DefaultCellStyle.ForeColor = Color.Red
                            r.DefaultCellStyle.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Strikeout)
                        ElseIf (r.Cells(9).Value) = "Active" Then
                            r.DefaultCellStyle.Font = New Font("Microsoft Sans Serif", 8)
                            r.DefaultCellStyle.BackColor = Color.Orange
                        End If
                    Next
Posted
Updated 16-Oct-18 4:30am

1 solution

Set the styles for the cell, rather than the default cell style:
VB.NET
For Each r As DataGridViewRow In frmCheckOut_Room.DataGridView2.Rows
    Dim cell As DataGridViewCell = r.Cells(9)
    If cell.Value = "Void" Then
        cell.Style.ForeColor = Color.Red
        cell.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Strikeout)
    ElseIf cell.Value = "Active" Then
        cell.Style.Font = New Font("Microsoft Sans Serif", 8)
        cell.Style.BackColor = Color.Orange
    End If
Next

NB: Since the style refers to disposable objects, it would probably be a good idea to store the styles in fields and reuse the same style instance.

Cell Styles in the Windows Forms DataGridView Control | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900