Datagrid - how to "ensurevisible" on a row?

R

Ron

I have a VB.NET project that is using the .NET Datagrid. I am
populating it via an ADO.NET Dataset. I was tentative about using the
Datagrid until I witnessed its speed in handling large amounts of data
- I was really wanting to use the ListView control because of my past
experience with it in VB6 & ADO, but the Listview unfortunately proved
too sluggish.

So far I'm happy with the Datagrid, except that I can't find the
Datagrid equivalent to the ListView's "Ensurevisible" method. For
example, if a user clicks a column to re-order the grid, the selected
row is often moved out of visible view. I want to ensure that the
selected row is kept in the grid's visible pane. In the Listview
control, you use the "Ensurevisible" method to accomplish this. Any
ideas on how to do the same within the Windows Forms Datagrid control?

Any help or code examples would be greatly appreciated.

Thanks in advance, and Happy Holidays!

Ron
 
R

Ron

I also need to know of the correct event to use to capture the
resorting of the grid, so that I can keep the selected row visible.

Thanks,

Ron
 
R

Ron

Well, I've discovered which events to monitor to make this happen (the
MouseDown and Paint events on the Datagrid), but I've yet to find the
correct Method in the Datagrid to navigate to my desired row. I
thought that NavigateTo would be it, but it's just not working. :(

I've stepped through the Code below, and the curCardID variable is
getting populated, and it's falling through my Paint code and
attempting the NavigateTo... but the record isn't brought into the
visible pane.

This is my code so far - any help is appreciated!:

'Form level variable:
Dim curCardID as Long


Private Sub dgCards_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles dgCards.MouseDown

Dim hitTest As DataGrid.HitTestInfo

' Use only left mouse button clicks.
If e.Button = MouseButtons.Left Then
' Set dataGrid equal to the object that called this event
handler.
dgCards = CType(sender, DataGrid)

' Perform a hit test to determine where the mousedown
event occured.
hitTest = dgCards.HitTest(e.X, e.Y)

' If the mousedown event occured on a column header,
' then perform the sorting operation.
If hitTest.Type = DataGrid.HitTestType.ColumnHeader Then
' Reset mouse button state.
buttonPress = True
curCardID = CLng(dgCards.Item(dgCards.CurrentRowIndex,
0))
End If
End If

End Sub



Private Sub dgCards_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles dgCards.Paint

If buttonPress Then
buttonPress = False

Dim x As Integer

For x = 0 To dsCards.Tables(0).Rows.Count - 1
If CLng(dgCards.Item(x, 0)) = curCardID Then
With dgCards
.Focus()
.NavigateTo(x, "Cards")
.Select(x)
Exit For
End With
End If
Next
End If

End Sub
 
R

Ron

GOT IT!

Instead of the "NavigateTo" method, I just use the CurrentRowIndex
property to assign the row index that I want. Like so:

For x = 0 To dsCards.Tables(0).Rows.Count - 1
If CLng(dgCards.Item(x, 0)) = curCardID Then
With dgCards
.Focus()
.CurrentRowIndex = x
.Select(x)
Exit For
End With
End If
Next

It works great!

Hopefully this might help someone else out there...

Happy Holidays,

Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top