Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagridview - change border instead of color for selected row?

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
Is there a relatively easy way to change how a datagridview formats a row when it's selected. To be more precise, I want to put a heavy border around the selected row and leave the SelectionBackColor at it's current setting instead of the default blue color.

I'm open to other methods for making the selected row stand out as long as it doesn't change the cell color. Changing the font size/style/color/etc. is also not desired.
 
In case anyone has the same problem, I found a solution to my problem here:
I converted the relevant part to VB and it works like I wanted.
Code:
    Private Sub HandleRowPrePaint(ByVal sender As Object, ByVal e As DataGridViewRowPrePaintEventArgs) Handles dgvJobList.RowPrePaint
        Dim state As DataGridViewElementStates = e.State And DataGridViewElementStates.Selected
        If state = DataGridViewElementStates.Selected Then
            e.PaintParts = e.PaintParts And Not (DataGridViewPaintParts.Focus Or DataGridViewPaintParts.SelectionBackground)
        End If
    End Sub

    Private Sub HandleRowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles dgvJobList.RowPostPaint
        Const intBorderWidth As Integer = 2
        Dim rowRect As Rectangle
        Dim state As DataGridViewElementStates
        state = e.State And DataGridViewElementStates.Selected
        If state = DataGridViewElementStates.Selected Then
            Dim iBorder As Integer = Convert.ToInt32(intBorderWidth)
            Dim columnsWidth As Integer = dgvJobList.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)
            Dim intRectStart As Integer = dgvJobList.RowHeadersWidth - 40
            rowRect = New Rectangle(intRectStart, e.RowBounds.Top + iBorder - 1, columnsWidth - dgvJobList.HorizontalScrollingOffset + 1, e.RowBounds.Height - iBorder)
            Using Pen As Pen = New Pen(Color.Red, intBorderWidth)
                e.Graphics.DrawRectangle(Pen, rowRect)
            End Using
        End If
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top