Datagrid

N

Nathan

I've written a procedure to select a certain row in a datagrid. After
finding the row, I use DataGrid.Select(RowNumber) to select the row.
However, I need the datagrid to scroll so that the newly selected row is
visible, preferably at the top. Is there a way to do this?

THANKS,

Nathan
 
A

Andy O'Neill

Nathan said:
I've written a procedure to select a certain row in a datagrid. After
finding the row, I use DataGrid.Select(RowNumber) to select the row.
However, I need the datagrid to scroll so that the newly selected row is
visible, preferably at the top. Is there a way to do this?

THANKS,

Nathan
The following scrolls a grid so the user sees the last row.
Can't recall where I got the code from but it' s based on someone else's.

========
Public Class MyDataGrid

Inherits Windows.Forms.DataGrid

Private oldSelectedRow As Integer

Sub ScrollToRow(ByVal row As Integer)

If Not Me.DataSource Is Nothing Then

Me.GridVScrolled(Me, _

New ScrollEventArgs(ScrollEventType.LargeIncrement, row))

End If

End Sub

Protected Overloads Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

'don't call the base class if left mouse down

If (e.Button <> MouseButtons.Left) Then

MyBase.OnMouseMove(e)

End If

End Sub

Protected Overloads Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)

'don't call the base class if in header

Dim hti As DataGrid.HitTestInfo

hti = Me.HitTest(New Point(e.X, e.Y))

If (hti.Type = DataGrid.HitTestType.Cell) Then

If (oldSelectedRow > -(1)) Then

Me.UnSelect(oldSelectedRow)

End If

oldSelectedRow = -(1)

MyBase.OnMouseDown(e)

Else

If (hti.Type = DataGrid.HitTestType.RowHeader) Then

If (oldSelectedRow > -(1)) Then

Me.UnSelect(oldSelectedRow)

End If

If ((Control.ModifierKeys And Keys.Shift) _

= 0) Then

MyBase.OnMouseDown(e)

Else

Me.CurrentCell = New DataGridCell(hti.Row, hti.Column)

End If

Me.Select(hti.Row)

oldSelectedRow = hti.Row

End If

If (hti.Type = DataGrid.HitTestType.ColumnHeader _

Or hti.Type = DataGrid.HitTestType.ColumnResize) Then

MyBase.OnMouseDown(e)

End If

End If



End Class


---------------
Public Sub SetFocus_Last(ByVal frm As Form, ByVal dt As DataTable, ByVal grd
As MyDataGrid)

Dim cm As CurrencyManager

Dim dv As New DataView

cm = CType(frm.BindingContext(dt), CurrencyManager)

dv = CType(cm.List, DataView)

If dv.Count > 0 Then

grd.ScrollToRow(dv.Count - 1)

End If

End Sub
 
G

Guest

use DataGrid.CurrentRowIndex=RowNumber, but it doesn't ganuartee at the top.

You can try use Protected Overridable Sub GridVScrolled to roll the row at
the top.
 

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