Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid HitTest problem

Status
Not open for further replies.

jebenson

Technical User
Joined
Feb 4, 2002
Messages
2,956
Location
US
Hello all,
I am using th following code to determine the row clicked in a DataGrid:

Code:
Private Sub DgMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dg.MouseUp

    Dim hit As System.Windows.Forms.DataGrid.HitTestInfo

    hit = sender.HitTest(e.X, e.Y)

    If hit.Type = Windows.Forms.DataGrid.HitTestType.Cell Or hit.Column = -1 Then
        sender.select(hit.Row)
    End If

End Sub

The problem comes when there are more rows than can be displayed in the visible portion of the grid. Say there are 5 rows, but only 3 of them are fully visible, the fourth is half-visible (half of the row is obscured) and the fifth row is not visible at all without scrolling. If I click on rows 1-3, there is no problem. But if I click on row 4 while it is still partially obscured by the botom of the grid, then "hit.Row" returns -1 and the code "sender.select(hit.Row)" throws an "Index was outside the bounds of the array" error. If I scroll such that the 4th row is fully visible then everything is ok.

I am fairly certain that the problem arises because clicking on a partially obscured row causes the grid to scroll to get that row fully visible, and this confuses the X and Y values passed to HitTest.

So my question is, how do I determine the row in this situation?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Anyone?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Okay, a little clarification of the problem. In the example I gave above, the code would not error out on clicking the fourth row. What it actually does is scroll the grid and select the 5th row, when what I wanted is the 4th. This confirms that the scrolling is indeed the problem, because it reads the XY location of the hit, scrolls the grid and then selects the row that corresponds to that XY location, which is now the row below the one I actually clicked. Basically, the row I want is moving out from under the click location before it can be selected.

If the obscured row is the last row in the grid, the behavior mentioned in the original post occurs (i.e., hit.Row returns -1, "index out of bounds", etc.).

So, I tried moving the code to the MouseDown event handler, but now the row does not get selected at all. I step through the code and it all executes - including the "sender.select(hit.Row)" - but the grid row does not get selected. The Click event is not an option because it does not fire until after a MouseDown and a MouseUp event, which is too late because by then the row has moved.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top