3

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.

user101010101
  • 1,609
  • 6
  • 31
  • 52

3 Answers3

5

In addition to what @blindmeis suggested

this._myview.MoveCurrentTo()

add this line:

dataGridView.ScrollIntoView(dataGridView.SelectedItem);
theartwebreathe
  • 361
  • 3
  • 6
0

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?

blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • Sorry I have added extra comments on my question, the user can see the row numbers. – user101010101 Jan 18 '12 at 14:33
  • As far as i know that will only move the selection and not actually move the view there. – H.B. Jan 18 '12 at 14:34
  • H.B. is right i miss to add the ScrollIntoView stuff. nevertheless if a datagrid has 300 rows then the row 250 is always the same, but when i use sorting the content of the row 250 change - if user101010101 the still wants the user to go to the row 250 with chaged content, then my approach is not the best. he sould then take SelectedIndex i think – blindmeis Jan 19 '12 at 06:38
0

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();
         }
      }
   }
}
Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
  • Thank you, I tried your code my only problem is when it scrolls, it dosent sent seem to move the scroll down enough. For example I set the rowNumber to 279 but only moved to 33. Any ideas? – user101010101 Jan 18 '12 at 15:39
  • Are you using physical or logical scrolling? See http://stackoverflow.com/questions/3062540/wpf-datagrid-cancontentscroll-property-causing-odd-behavior for reference. The kludge method (which is what I assume you tried) requires logical scrolling. – Esoteric Screen Name Jan 18 '12 at 15:51
  • thanks, I am very confused, any number including and under 31 works correctly but any number larger does not jump any further then 31 do you have any ideas? – user101010101 Jan 18 '12 at 16:02
  • What is `myGrid.Items.Count`? If you've only 32 items in your grid, you can't jump past row 31 (0 based indexing). If that's not the answer, I don't think I'll be much more help without seeing more of your code. – Esoteric Screen Name Jan 18 '12 at 16:05
  • no scroll count is 370 I really cant tell wot is going on. Do I need any properties set in my datagrid? – user101010101 Jan 18 '12 at 16:09
  • `CanContentScroll=true` might do it. If not, I advise that you abandon this method and instead use the `myGrid.ScrollIntoView(myGrid.SelectedItem)`. The for-loop kludge was just an off the cuff alternative, and is not the best approach for this problem. – Esoteric Screen Name Jan 18 '12 at 16:20