Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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.
Posted 16-Jun-12 2:24am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
        MyBase.OnPaint(e)
 
        ' 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.
  Permalink  
Comments
Csharp_Learner at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 230
2 BillWoodruff 205
3 Sergey Alexandrovich Kryukov 195
4 DamithSL 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 16 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100