Link to home
Start Free TrialLog in
Avatar of printmedia
printmedia

asked on

Datagridview control last row back color red vb.net

Hi all.

I would like to change the back color of the last row in my datagridview control to red. I've tried doing it in the CellFormatting event but no luck:

 Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        With DataGridView1

            If Not e.RowIndex Mod 2 = 0 AndAlso e.ColumnIndex >= 0 AndAlso e.ColumnIndex < 8 Then
                e.CellStyle.BackColor = Color.Beige
            ElseIf Not e.RowIndex Mod 2 = 0 AndAlso e.ColumnIndex = 8 Then
                e.CellStyle.BackColor = Color.MistyRose
            ElseIf Not e.RowIndex Mod 2 = 0 AndAlso e.ColumnIndex > 8 Then
                e.CellStyle.BackColor = Color.Beige
            Else
                e.CellStyle.BackColor = Color.White
            End If

DataGridView1.Rows(DataGridView1.Rows.Count - 1).DefaultCellStyle.BackColor = Color.Red

End With
    End Sub

Open in new window


I also tried adding the code to the button click event that loads the data into the datagridview but still no luck:

Private Sub btnRun_Click(sender As Object, e As EventArgs) Handles btnRun.Click
DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True     'Word wrap 
        DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells  'Adjust the height of the rows
        DataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter    'Center in the middle cell contents
        DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter   'Center in the middle the column headers

DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText

        DataGridView1.Columns(0).Width = 180
        DataGridView1.Columns(2).Width = 115
        DataGridView1.Columns(3).Width = 130
        DataGridView1.Columns(4).Width = 120
        DataGridView1.Columns(5).Width = 120

        DataGridView1.Rows(DataGridView1.Rows.Count - 1).DefaultCellStyle.Font = New Font("Arial", 12, FontStyle.Bold)
        DataGridView1.Rows(DataGridView1.Rows.Count - 1).DefaultCellStyle.BackColor = Color.Red

End Sub

Open in new window


Any idea what I can do to get this to work? Thank you in advance.
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

don't use DefaultCellStyle use, style instead.

you can loop over all cells if you need to:
DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(ColIndex).Style.BackColor = Color.Red

Open in new window

I've always used DefaultCellStyle, just like you did in the first code snippet.

You might also try the DataBindingComplete event.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of printmedia
printmedia

ASKER

Thank you.