DataGridView Selection Behavior

jeanette7

Member
Joined
Mar 18, 2007
Messages
5
Programming Experience
Beginner
Hi there,

Instead of changing the backcolor of the cell upon selection, I would like the border size/color to change (like in Excel). How would I accomplish this?

Your help is much appreciated :)

Jeanette
 
Try the CellPainting event. Example:
VB.NET:
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    If e.ColumnIndex <> -1 AndAlso e.RowIndex <> -1 Then
        Dim current As DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)
        If current.Selected Then
            e.Paint(e.CellBounds, e.PaintParts)
            Using p As New Pen(Color.Red, 1)
                Dim rct As Rectangle = e.CellBounds
                rct.Offset(-1, -1)
                rct.Inflate(-1, -1)
                e.Graphics.DrawRectangle(p, rct)
            End Using
            e.Handled = True
        End If
    End If
End Sub
Also check this article: How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance
 
Back
Top