[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1204
  • Last Modified:

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.
  • 2
1 Solution
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

Line 18 should be:
e.Graphics.DrawLine(p, lineFrom, lineTo)
PizpotAuthor Commented:
I am trying that snippet thanks...

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now