none
Datagrid: Scrolling to a row - ScrollIntoView not working

    Question

  •  Alrighty, this has taken me almost all day and I've gotten nowhere!  I was hoping that maybe posting my problem someone may be able to help :)

    First off, we're running SL2 [we have not yet migrated to SL3].  What I am looking to do is this: I want to get a ton of data that will be sorted in ascending order based on a timestamp in the first column with values in secondary columns [they aren't important].  This data will be coming in at different times, so the collection will be changing.

    When new data comes into the collection I check to see if a condition is met, and if it is I want to select the corresponding row in the datagrid and then have the datagrid scroll down to show the row [since it may not be onscreen].

    I seem to be able to do the selecting of the row just dandy... however, I cannot for the life of me get my grid to scroll to show it.  I have tried using the ScrollIntoView function, but I keep getting the exception "Invalid row index. Cannot complete operation" when trying to run the application.

    This is the line as written that it complains about; I can try to post more code if need be, but things are a little all over at the moment as I've been trying everything.

    __TabDataGrid.ScrollIntoView(__TabDataGrid.SelectedItem, __TabDataGrid.Columns[0]);

    I have checked all of my variables time and time again, __TabDataGrid.SelectedItem seems correct as does the column, so I am clueless.

     

    Has anyone ran into this before?  My google-foo has turned up nothing.

    Thanks in advance


    ps.  I've tried calling __TabDataGrid.UpdateLayout(); before the scrollIntoView function as suggested here [http://forums.silverlight.net/forums/t/60491.aspx] and it still gives the same error.


     

     

     

     

     

    Monday, September 28, 2009 6:50 PM

Answers

  • When and where (in your lifecycle) are you trying to ScrollIntoView? Try using

    _TabDataGrid.Dispatcher.BeginInvoke(delegate {

    _TabDataGrid.Focus();

    _TabDataGrid.SelectedItem = whatever;

    _TabDataGrid.CurrentColumn = _TabDataGrid.Columns[0];

    _TabDataGrid.ScrollIntoView(_TabDataGrid.SelectedItem,_TabDataGrid.Columns[0]);

    });

    You may not need all of that but that is what I use. Dispatcher being the main part. Just be sure the current column and selecteditem have values. This is in SL3 but may apply.

    Tuesday, September 29, 2009 12:05 PM

All replies

  • I've used that feature succesfully and here's how I've done it:

    Grid.SelectedIndex = 5; // the index you want to select
    Grid.UpdateLayout();
    Grid.ScrollIntoView(Grid.SelectedItem, 0);  // I couldn't get the column selection to work

    Hope this helps in your case too. I think the UpdateLayout is the key. You said you tried using that too so if it's still not working then I don't know what could be causing that. This works for me but I'm using SL3 although if I remember correctly, I used this same code with SL2 also.

    Tuesday, September 29, 2009 1:42 AM
  • I have not used SL2, but I have noted in SL3 the grid needs focus.

    Tuesday, September 29, 2009 9:04 AM
  • Yeah, I did try adding UpdateLayout to no success.

    Something weird I just found out though...  I'm thinking perhaps my issue may be partially due to the column setting?  I tried the following:

    __TabDataGrid.ScrollIntoView(__TabDataGrid.SelectedItem, __TabDataGrid.Columns[0]);  <-- Original try, see main post above. __TabDataGrid.ScrollIntoView(__TabDataGrid.SelectedItem, 0); <-- Cannot not compile, int is not is assignable to the datagridcolumn type
    __TabDataGrid.ScrollIntoView(__TabDataGrid.SelectedItem, null);  <-- COMPILED!  But the scroll still did not work.  :(

    Has anyone seen this and been able to fix it?

     

    Tuesday, September 29, 2009 11:17 AM
  • When and where (in your lifecycle) are you trying to ScrollIntoView? Try using

    _TabDataGrid.Dispatcher.BeginInvoke(delegate {

    _TabDataGrid.Focus();

    _TabDataGrid.SelectedItem = whatever;

    _TabDataGrid.CurrentColumn = _TabDataGrid.Columns[0];

    _TabDataGrid.ScrollIntoView(_TabDataGrid.SelectedItem,_TabDataGrid.Columns[0]);

    });

    You may not need all of that but that is what I use. Dispatcher being the main part. Just be sure the current column and selecteditem have values. This is in SL3 but may apply.

    Tuesday, September 29, 2009 12:05 PM
  • Yay!  That worked!

    I read in this post that changes were made to how the rows were being created and so this was probably the cause of the row not being valid [even though in my IDE it looked like it was].  They suggested calling the function in the loaded event, but given the bug with it being fired at an incorrect time I couldn't even do this.  I had forgotten about the use of Dispatcher being able to call my function at an applicable time!

     

    Thank you so so so much!

    Tuesday, September 29, 2009 2:21 PM
  • I just updated to SL3 and I'm back getting the same exception. None of the proposed solutions [Updatelayout, focus, dispatcher] are fixing it for me this time :(

    Has anyone revisited this issue?

     

    Monday, November 30, 2009 5:24 PM
  • When using Dispatcher I have not been able to reproduce the error. I would double check your timing.

    Monday, November 30, 2009 5:35 PM
  • My work around was to add and error handler and ignore the error...

    Private Sub selectIndexForDataGrid(ByVal selectedFeature As Graphic, ByVal stateFactList As ObservableCollection(Of StateFacts))
        On Error GoTo ErrorHandler
        For Each state In stateFactList
          If state.StateName = selectedFeature.Attributes("states.STATE_NAME") Then
            dataGrid.SelectedIndex = stateFactList.IndexOf(state)
            dataGrid.UpdateLayout()
            tmrScrollIntoView.Start()
          End If
        Next
        dataGrid.ScrollIntoView(dataGrid.SelectedItem, dataGrid.Columns(0))
    ErrorHandler:

    Wednesday, April 27, 2011 11:34 AM