Reputation: 1659
Can someone please help, I am trying to create a method that will allow a user to type a row number in and the method will 'jump' them to that row. When the row number is passed in for instance.
I am using a dataTable as the item source for the datagrid.
The user can see the row numbers on the side of the datagrid.
Exmaple: User types in row 250 and the datagrid is moved to that row.
I am using the WPF datagrid and c# 4.0.
Upvotes: 3
Views: 4688
Reputation: 6112
This should work:
After setting the selected item as Blindmeis suggests (or by whatever method you prefer), use myGrid.ScrollIntoView(myGrid.SelectedItem)
. Note that this may require tweaking based on how your UI handles selection on the grid.
Alternatively, an untested kludge:
protected void JumpToRow(int rowNumber)
{
if(rowNumber>=0 && myGrid.Items!=null && rowNumber<myGrid.Items.Count)
{
var border = VisualTreeHelper.GetChild(myGrid, 0) as Decorator;
if (border != null)
{
var scroll = border.Child as ScrollViewer;
if (scroll != null) scroll.ScrollToEnd();
scroll.ScrollToStart();
for(int i=0;i<rowNumber;++i)
{
scroll.LineDown();
}
}
}
}
Upvotes: 0
Reputation: 371
In addition to what @blindmeis suggested
this._myview.MoveCurrentTo()
add this line:
dataGridView.ScrollIntoView(dataGridView.SelectedItem);
Upvotes: 6
Reputation: 22445
i use the ICollectionView MoveCurrentTo methods.
this._myview.MoveCurrentTo()
the good thing is that ICollectionView is aware of sorting and so on.
maybe you tell us what your itemssource is and why the user has to know the row "index". does the user count the rows on the screen? or to you want the user to get to the row with the Key=250?
Upvotes: 0