# datagridview offsets to cell off screen?

How do I find the x/y offsets to a cell that is off screen?
###### Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x

Commented:
Hi Pizpot! I had some fun creating a clipping function aswell. Heres the new improved DataGridView1_Paint :
``````    Private Sub DataGridView1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
Dim c1, c2, r1, r2 As Int16

' from cell 1
c1 = 1 : r1 = 15

' to cell 2
c2 = 2 : r2 = 30

If DataGridView1.Rows.Count >= r2 - 1 Then

Dim p As Pen = New Pen(Color.Red)
Dim rectCell1 As Rectangle, rectCell2 As Rectangle

rectCell1 = DataGridView1.GetCellDisplayRectangle(c1, r1, False)
rectCell2 = DataGridView1.GetCellDisplayRectangle(c2, r2, False)

Dim r As Int16
Dim c As Int16
Dim ro As Integer = 0
Dim co As Integer = 0

' Calculate the distance between cell 1 and cell 2
For r = r1 To r2 - 1
ro = ro + DataGridView1.Rows(r).Height
Next
For c = c1 To c2 - 1
co = co + DataGridView1.Columns(c).Width
Next

Dim lineTo As Point
Dim lineFrom As Point

' Calculate the coordinates of the non-visible cell
If (rectCell2.X = 0 And rectCell2.Y = 0) Then
' Cell 2 is invisible

If (rectCell1.X = 0 And rectCell1.Y = 0) Then
' Both cells are invisible - no line
Exit Sub
End If
rectCell2.X = rectCell1.X + co
rectCell2.Y = rectCell1.Y + ro
rectCell2.Height = DataGridView1.Rows(r2).Height
rectCell2.Width = DataGridView1.Columns(c2).Width
End If
If (rectCell1.X = 0 And rectCell1.Y = 0) Then

' Cell 1 is invisible
rectCell1.X = rectCell2.X - co
rectCell1.Y = rectCell2.Y - ro
rectCell1.Height = DataGridView1.Rows(r1).Height
rectCell1.Width = DataGridView1.Columns(c1).Width
End If

' From cell 1 left edge
lineFrom.X = rectCell1.X
lineFrom.Y = rectCell1.Y + rectCell1.Height / 2

' To cell 2 right edge
lineTo.X = rectCell2.X + rectCell2.Width
lineTo.Y = rectCell2.Y + rectCell2.Height / 2

Dim g As Graphics
g = Me.DataGridView1.CreateGraphics

' Sett clipping region to be inside the griddata, exclude headers

' Draw the line with clipping
g.DrawLine(p, lineFrom, lineTo)
g.Dispose()

End If

End Sub
``````
0

Author Commented:
I mean scrolled out of view, not off screen.
0

Commented:
Just a single cell is off the screen? I think I need more information.  Could you perhaps post the code that is giving you the issue?  Thanks
0

Author Commented:
If a user clicks on a cell, I want to be able to draw a line to another cell whether it is scrolled and not visible, or not. Here is a link to my last question/answer, which handles the case where one cell is not visible. But once that cell is not visible, it gets drawn to 0,0.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23127884.html

0

Author Commented:
edit last post:

If a user clicks on a cell, I want to be able to draw a line to another cell whether it is scrolled and not visible, or not. Here is a link to my last question/answer, which handles the case where both cells are visible.

If you try it when the 2nd cell is not visible, the line it to 0,0.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23127884.html
0

Author Commented:
I think I am just going to estimate the pixel offsets to off screen cells. Plus or minus a fex pixels in the y axis will not be noticable.
0

Author Commented:
interesting... :-)
0

Commented:
Yes, isn't it? :-) Did it work as you expected?
0

Author Commented:
Yes it did, thanks very much. I will even use it if both cells are not visible because that can happen as you scroll around. :-)
0

Author Commented:
Yes it did, thanks very much. I will even use it if both cells are not visible because that can happen as you scroll around. :-)
0