I have an editable DataGridView with SelectionMode set to FullRowSelect (so the whole row is highlighted when the user clicks on any cell). However I would like the cell that currently has focus to be highlighted with a different back color (so the user can clearly see what cell they are about to edit). How can I do this (I do not want to change the SelectionMode)?
views:
2395answers:
2
A:
You want to use the DataGridView RowPostPaint method. Let the framework draw the row, and afterwards go back and color in the cell you're interested in.
An example is here at MSDN
clintp
2008-09-16 15:33:18
Thanks for this. As I understand it, however, the MSDN article seems to be suggesting that I should be using the RowPrePaint method. Even so, though, I still can't figure out the right code to use.
Phillip Wells
2008-09-16 16:12:34
+2
A:
I figured out a better way of doing this, using the CellFormatting event:
Private Sub uxContacts_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles uxContacts.CellFormatting
If uxContacts.CurrentCell IsNot Nothing Then
If e.RowIndex = uxContacts.CurrentCell.RowIndex And e.ColumnIndex = uxContacts.CurrentCell.ColumnIndex Then
e.CellStyle.SelectionBackColor = Color.SteelBlue
Else
e.CellStyle.SelectionBackColor = uxContacts.DefaultCellStyle.SelectionBackColor
End If
End If
End Sub
Phillip Wells
2008-09-16 16:30:07