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:
I also tried adding the code to the button click event that loads the data into the datagridview but still no luck:
Any idea what I can do to get this to work? Thank you in advance.
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
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
Any idea what I can do to get this to work? Thank you in advance.
I've always used DefaultCellStyle, just like you did in the first code snippet.
You might also try the DataBindingComplete event.
You might also try the DataBindingComplete event.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.
you can loop over all cells if you need to:
Open in new window