Question How to modify the defaule fore color

viper5646

Active member
Joined
Jan 20, 2009
Messages
38
Location
Canada
Programming Experience
Beginner
Hi all
I have a datagridview where one of the columns display the amount.
I have been trying to change the color of the amount to red if the value is 0 or negative.
With all my tries have been negative.
With the following Code I receive This error
"NullRefferenceException was unhandled".
VB.NET:
    Private Sub redifnegative()

        For x As Integer = 0 To (Utilitiesgridview.RowCount) - 1
            Dim w As Double = CDbl(Utilitiesgridview.Rows(x).Cells(1).FormattedValue)
            
            If w < 0 Then
                Utilitiesgridview.Rows(x).Cells(1).ContextMenuStrip.ForeColor = Color.Red
            End If
        Next

    End Sub
Thanks
 
VB.NET:
Private Sub DataGridView1_CellFormatting(ByVal sender As Object, _
                                         ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If e.ColumnIndex = 0 Then
        If CInt(e.Value) <= 0 Then
            e.CellStyle.ForeColor = Color.Red
        End If
    End If
End Sub
 
Back
Top