Drawline with e.CellBounds.Bottom causes blinking RRS feed

  • Question

  • Hi all,
    I'm using a DataGridView and have implemented drag and drop reordering of the rows in the grid. I added a Drawline to show where the row will be dropped if released which depends on whether the row is being dragged above or below it's current position in the grid. the code I'm using to draw the line is as follows:

    line = New Pen(Color.Red, 2) 'Defined globally and set elsewhere

    Private Sub dataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

            If rowIndexOnMouseDrag > rowIndexFromMouseDown Then 
                e.Graphics.DrawLine(line, 0, e.CellBounds.Bottom - 1, DataGridView1.Width, e.CellBounds.Bottom - 1)

            ElseIf rowIndexOnMouseDrag < rowIndexFromMouseDown Then 
                e.Graphics.DrawLine(line, 0, e.CellBounds.Top - 1, DataGridView1.Width, e.CellBounds.Top - 1)

            End If

    End Sub

    The rowIndexOnMouseDrag < rowIndexFromMouseDown ElseIf using e.CellBounds.Top works just fine, it draws the full line and it stays visible for as long as it should be visible...
    The rowIndexOnMouseDrag > rowIndexFromMouseDown using e.CellBounds.Bottom however blinks excesively and doesn't always draw the full line (the right most cell in the row doesn't draw every blink)

    I slowed down the blinking by adding a bit of code to my DragOver event to only refresh the grid if the cursor has moved a certain amount but I can't seem to find a fix for the line not being fully drawn and I was wondering if anyone can think of why this would be happening?

    Monday, December 15, 2008 6:42 PM


  • Hi NDSP,

    Base on my test, it really has problem with
    "e.Graphics.DrawLine(line, 0, e.CellBounds.Bottom - 1, DataGridView1.Width, e.CellBounds.Bottom - 1)".

    Since DataGridView will paint its cell one by one, current row will be painted after the previous row. When you draw the line at the top of the current row. the line will be painted at the bottom of the previous row. If you draw the line at the bottom of the current row, it will be covered by the cell because the cell is painted after the line. I suggest you to paint the line at the top of the current row and use different color for drag above or below.

    If you have any question, please feel free to tell me.

    Kira Qian

    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by Kira Qian Monday, December 22, 2008 2:44 AM
    Wednesday, December 17, 2008 9:15 AM