Click here to Skip to main content
15,068,645 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a datagridview in a Windows Forms application. On this datagridview I want to show a connector line (to represent grouping between some rows) from say 2 row to 4 row. This is for example purpose, in actual requirement based on the data in the datagridview the starting and ending row will be taken.

Now, the question is I want to draw this line on a separate surface like layer. So, the datagridview will be in one layer, and the lines are to be drawn on another layer, so that this layer can be turned off.

I am thinking of doing the above thing using the Panel control as shown in this article How to Use Transparent Images and Labels in Windows Forms[^] setting the panel to match the size of the datagridview and drawing in the paint event of datagridview so that whenever the datagridview is redrawn the panel is also redrawn.

Can you please tell whether my approach is correct, or is there any other better method.

Thank you in advance.

1 solution

You don't need the Panel at all.

Create your own DGV class by inheriting from it, then override the OnPaint method with something like:
Public Class MyDGV
    Inherits DataGridView

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)

        ' Call your line painting code here...
    End Sub

    Private Sub MyLinePaintingCode()
        Dim g As Graphics = Me.CreateGraphics

        ' Your painting logic goes here...
    End Sub
End Class

Then you don't have to use a Panel control and worry about the messy details of Transparency in Windows Forms.

Your custom DGV will show up in the Toolbox.  Use that one instead of the default DGV control and your done.
Csharp_Learner 16-Jun-12 12:11pm
Thank you very much for the answer.I tried your solution like

using (var gr = dataGridView1.CreateGraphics()) {

gr.DrawLine(Pens.Red, 0, 0, dataGridView1.Width, dataGridView1.Height);

}in the Paint event and faced the following two problems

1. The line is draw below headers and scroll bars
I want a top most drawing surface, so that I can put some marks in the headers also.

2. When the scroll bar is moved several lines are drawn, since the earlier drawn is not erased.

Can you please suggest the solution for the above problems.Thanks in advance.
Dave Kreskowiak 16-Jun-12 16:05pm
Your description didn't say anything about drawing in the scrollbar or header area.

You can't draw in the area of the scroll bars. It's a huge pain and gets very messy and you won't like the flickering result you're going to get. In order to make room for painting over there, you'd probably make the control width a bit wider than the total width of your columns to give you room to paint with the scroll bar out of the way.

Headers is a slightly different story, but it's still a pain. You can override OnRowPostPaint method and if I remember correctly, the RowIndex is going to be -1 for the column headers.

Using a transparent control over the top of the DGV introduces more problems than it solves. Keeping the lines in sync with the scrolling of the DGV, determining where to paint based on cell coordinates, and the DGV won't receive mouse events because they are all going to the Panel above the DGV are the ones I can think of off the top of my head.

Csharp_Learner 16-Jun-12 23:38pm
Thank you for the reply. As you said drawing on transparent panel may be more problamatic.

I will try with your solution.

Thank you very much for the guidance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900