Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to change gridview cell background.

I want to change using different three color, depending on cell data text.

if text of gridview cell is "High", I want to set red color, or

if text of gridveiw cell is "Moderate", I want to set yellow color. or

if text is "Low", I want to set Green color.

I binded gridview with Access datasource.

here is my code:
VB
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles ActionsLogDataGridView.CellFormatting

    For j = 0 To Me.ActionsLogDataGridView.RowCount - 1
        Dim Cell = Me.ActionsLogDataGridView.Rows(j).Cells.Item("ActionImportance")
        Dim col As Color

        Select Case Cell.Value.ToString
            Case "Low"
                col = Color.Green
            Case "Moderate"
                col = Color.Yellow
            Case "high"
                col = Color.Red
        End Select

        Cell.Style.BackColor = col
    Next
End Sub

thanks...
Posted
Updated 24-May-15 9:44am
v2
Comments
Sergey Alexandrovich Kryukov 24-May-15 16:02pm    
All right. There is no such column.
—SA
Sherif Adely 25-May-15 13:44pm    
Hi Sergey ,
I already have this column "ActionImportance" under MS Access database table "Actionslog"
kashif Atiq 25-May-15 1:59am    
This is my wild guess as i never worked with datagridview. But it might be because your code trying to find "ActionImportance" in header row? try setting j = 1 maybe that is the problem
Sherif Adely 25-May-15 13:46pm    
Hi Kashif,
I changed the value of J to 1 instead of 0 but I have the same error. Thanks

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