datagridview, how to change color RRS feed

  • Question

  • i have an exercise and i want to choosen some row on datagridview with red color, those rows will be filtered by some condition? how can i change color of those rows.
    thanks for your interesting
    Sunday, May 27, 2007 12:32 PM


  • Hi,


    You need to handle the CellFormatting event of the data grid view. This event is raised for every visible cell in the datagridview. A System.Windows.Forms.DataGridViewCellFormattingEventArgs object gets passed into the event. This has properties that you can use to determine if the cell needs to be formatted, and also how it should be formatted. This example formats all the cells that are in a column whose name ends with the word ACTION.


    Just remember the CellFormatting event is raised for every visible cell (column and row) in the datagridview.


    Code Snippet

     Private Sub dgrid_CellFormatting(ByVal sender As System.Object, _

            ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgrid.CellFormatting


      If Me.dgrid.Columns(e.ColumnIndex).DataPropertyName.EndsWith("ACTION") = True Then
                'action column formatting
                If e.Value >= 100 Then
                    e.CellStyle.BackColor = Color.Salmon
                ElseIf e.Value < 100 Then
                    e.CellStyle.BackColor = Color.LightGreen
                    e.CellStyle.BackColor = Color.White
                End If
                e.FormattingApplied = True
        End If


    End Sub



    Sunday, May 27, 2007 1:18 PM