0

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

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.