draw line from datagridview cell to another celll

Let's say I have a datagridview with 4 cols and 100 rows. If I scroll down and then click on cell(89,3), how do I draw a line connecting the left edge of that cell to the right edge of cell cell(49,0)?

I'd also like it to update if I scroll around, resize the form, min/maximise etc.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi, you would paint a line in datagridview_paint. Something like this. Set c1,r1 and c2, r2.
 Private Sub DataGridView1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
  If DataGridView1.Columns(c1).Visible And DataGridView1.Rows(r1).Visible And  DataGridView1.Columns(c2).Visible And DataGridView1.Rows(r2).Visible Then
   Dim p As Pen = New Pen(Color.Red)
   Dim rectCell1 As Rectangle, rectCell2 As Rectangle
   rectCell1 = DataGridView1.GetCellDisplayRectangle(c1, r1, True)
   rectCell2 = DataGridView1.GetCellDisplayRectangle(c2, r2, True)
   Dim lineFrom As Point
   lineFrom.X = rectCell1.X
   lineFrom.Y = rectCell1.Y + rectCell1.Height / 2
   Dim lineTo As Point
   lineTo.X = rectCell2.X + rectCell2.Width
   lineTo.Y = rectCell2.Y + rectCell2.Height / 2
   e.Graphics.DrawLine(p, rectCell1.X, rectCell1.Y, rectCell2.X, rectCell2.Y)
  End If
 End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Line 18 should be:
e.Graphics.DrawLine(p, lineFrom, lineTo)
PizpotAuthor Commented:
I am trying that snippet thanks...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.