Hi
I have a datagridview which I would like to change the the color of the text in a cell if the number is negative in the Total column.
This is what I have so far.

Dim rowcont As Integer = (TTLGrid.Rows.Count) - 1
        For x = 0 To rowcont
            Dim Total As String = TTLGrid.Rows(x).Cells(4).FormattedValue
            Dim PosNeg As String = Microsoft.VisualBasic.Left(Total, 1)
            If PosNeg = "-" Then
??
            End If

Thank You

Handle the CellFormatting event,

Sub Sample()
        Dim dt As New DataTable
        dt.Columns.Add("No", GetType(Integer))

        dt.Rows.Add(10)
        dt.Rows.Add(-1)
        dt.Rows.Add(22)
        dt.Rows.Add(-12)

        DataGridView1.AutoGenerateColumns = False
        Dim Col1 As New DataGridViewTextBoxColumn
        Col1.DataPropertyName = "No"
        AddHandler DataGridView1.CellFormatting, AddressOf Test1

        DataGridView1.Columns.Add(Col1)
        DataGridView1.DataSource = dt
    End Sub

    Sub Test1(ByVal s As Object, ByVal e As DataGridViewCellFormattingEventArgs)
        If e.ColumnIndex = 0 Then
            If e.Value < 0 Then
                e.CellStyle.ForeColor = Color.Red
            Else
                e.CellStyle.ForeColor = Color.Blue
            End If
        End If
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.