ScrollIndexIntoView - Index was outside of bounds of the array

0 Answers 99 Views
DataGrid
Tyler
Top achievements
Rank 1
Tyler asked on 04 Aug 2023, 05:07 PM

My App Center crash logs show a crash on ScrollIndexIntoView on a RadDataGrid. I've seen the crash about a dozen times in the last month on a variety of computers, but haven't been able to duplicate it myself. There are a variety of spots in my project where I scroll to particular entry on a page that the user previously clicked on. My code usually looks something like this. Assume ClientGrid is the RadDataGrid and its ItemSource is ViewModel.Clients.

foreach (var eachClient in ViewModel.Clients)
{
    if (eachClient.ID == SelectedClient.ID)
    {
        try
        {
            ClientGrid.ScrollItemIntoView(eachClient);
        }
        catch (Exception ex)
        {
            // log error
        }
        break;
    }
}

This works perfectly for me when I use it, and I suppose it does for everyone else 99% of the time. It just occasionally crashes. Note, sometimes in my project, I use ScrollIndexIntoView and other times I use ScrollItemIntoView. Both of them ultimately call ScrollIndexIntoView in the Telerik code. I can't actually tell from the App Center crash logs if the crash is from one of my ScrollItemIntoView or ScrollIndexIntoView calls. They follow this same code structure though.

Also, I tried wrapping the ScrollItemIntoView call inside a try/catch, but it's never actually hit the catch block (I didn't include it above, but I have some error reporting code in the catch) and doesn't prevent the crashes.

Is there some way to prevent this from crashing? I assume that somehow the grid just isn't ready to scroll yet. Is there a property on the grid I can call to verify that it's loaded before I call ScrollItemIntoView? It's fine with me if on these rare occasions I can't scroll to the appropriate row. I just want it to do nothing instead of crashing. Here's the stack trace

Internal.Runtime.CompilerHelpers    ThrowHelpers.ThrowIndexOutOfRangeException ()
Telerik.UI.Xaml.Controls.Grid    IndexStorage.ValueForIndex (Int32, Boolean)
Telerik.UI.Xaml.Controls.Grid.Model    GridModel.ScrollIndexIntoView (ScrollIntoViewOperation`1)
Telerik.UI.Xaml.Controls.Grid.Model.GridModel    <>c__DisplayClass147_0.<ScrollIndexIntoViewCore>b__0 ()
System    Action.Invoke ()
Telerik.UI.Xaml.Controls.Primitives    DelegateUpdate`1.Process ()
Telerik.UI.Xaml.Controls.Primitives    UpdateServiceBase`1.ProcessUpdatesQueue ()
Telerik.UI.Xaml.Controls.Primitives    UpdateServiceBase`1.OnUpdateCallback ()
Windows.Foundation    DeferralCompletedHandler.Invoke ()

 

Thanks for any help!

Didi
Telerik team
commented on 07 Aug 2023, 06:32 AM

Hi Tyler,

Thank you for the code and stack-trace.

We cannot say why the exception occurs without reproducing and researching it on our side.

If you can isolate the issue in a sample app, and send it back to us, that could be of great help. The exception may occur if you scroll to an item that does not exist yet. 

You can use

        var data= this.grid.GetDataView();

In short, GetDataView method provides a view of the ItemsSource after all the Sort, Group and Filter operations are applied. 

or using the ItemsSource and check whether the source is 1=null and >0, etc. It is up to you what to use based on the scenario you have.

Tyler
Top achievements
Rank 1
commented on 07 Aug 2023, 06:51 PM

Hi Didi,

Thanks for your response! I can't really make a simplified sample app with the error because I can't trigger the crash myself. I've only seen it on the crash logs from other people using our production app, so I won't be able to confirm it would happen in a sample app that I give you.

The GetDataView() call looks like good advice. However, it's not working the way I expected. After I call GetDataView(), I assume the IsDataReady property would be best way to check on whether it's safe to call ScrollItemIntoView. When I try this in my code, IsDataReady evaluates to false, which is strange because I can normally call ScrollItemIntoView without issue. If I wait a second later, then click a button on the page to check the data properties, IsDataReady evaluates to true. I suppose I could check on some sort of delay. It just doesn't seem ideal.

Using ItemsSource seems like it could work though. I tried setting the code up like this.

ObservableCollection<Client> itemsSource = (ObservableCollection<Client>)ClientGrid.ItemsSource;
if (itemsSource != null && itemsSource.Count == ViewModel.Clients.Count)
{
    clientGrid.ScrollItemIntoView(eachClient);
}

This works as expected. I'm not sure if it will help protect against the crash or not. If it doesn't, I guess I'll try the GetDataView() approach on some sort of delay.

Thanks for your help!

No answers yet. Maybe you can help?

Tags
DataGrid
Asked by
Tyler
Top achievements
Rank 1
Share this question
or