We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to position the DataPager to a given record

Hi,
I have a List<RecordData> that I display using an SFDataGrid and SFDataPager. That works fine. 
When I am given a RecordID for a RecordData, how can I position the DataPager to a page that contains that record and then select the record?

One way to find the index of the RecordData in the List is:
int index = recordDataList.FindIndex(obj => obj.recordID == incomingRecordID);
How do I turn that into a Page and a selected record?

And what happens if the DataGrid has been sorted? How can I select the record then?

Thanks!
Will

5 Replies

SV Srinivasan Vasu Syncfusion Team May 4, 2016 05:47 PM UTC

Hi Will, 
 
Thanks for contacting Syncfusion support. 
 
We have analyzed your query and you can achieve your requirement by calculating the PageIndex usging SfDataPager.PageSize property. By getting the PagedCollectionView.InternalList It is working same as for while sorting the SfDataGrid. Please refer the below code example. 
 
C# 
private void Button_Click(object sender, RoutedEventArgs e) 
        {             
            var collection = (this.dataGrid.View as PagedCollectionView).GetInternalList(); 
            int i = 0; 
            foreach (var rec in collection) 
            { 
                var value = (rec as OrderInfo).RecordID; 
 
                if ((int)value == 1119)//given RecordID 
                { 
                    int index = i / this.sfDataPager.PageSize; 
                    // Move to given record page 
                    this.sfDataPager.PageIndex = index; 
                    // Select the given record 
                    this.dataGrid.SelectedIndex = i % this.sfDataPager.PageSize; 
                    var rowindex = this.dataGrid.ResolveToRowIndex(this.dataGrid.SelectedIndex); 
                    var visualcontainer = this.dataGrid.GetVisualContainer(); 
                    visualcontainer.ScrollRows.ScrollInView(rowindex); 
                    break; 
                } 
                i++; 
            } 
        } 
 
Please download sample from the below location. 
 
 
Regards, 
Srinivasan 
 



WA Will Autio May 4, 2016 11:02 PM UTC

Hi Srinivasan,
Thanks for the code demo. 
I copied it to my code in a OnNavigatedTo method, changed some variables and ran it.Unfortunately, in:
 var collection = (this.dataGrid.View asPagedCollectionView).GetInternalList(); 
this.dataGrid.View is null. 
What would cause that? Could it be that this.dataGrid.View is not fully set up yet?
Thanks,
Will


SV Srinivasan Vasu Syncfusion Team May 5, 2016 10:58 AM UTC

HI Will, 
 
We have analyzed your query and when Navigating from one page to another page, the SfDataGrid.View will not be created while using OnNavigatedTo method. Instead of OnNavigatedTo method, you can use SfDataGrid.Loaded event to achieve your requirement.  
 
Please refer the below code example. 
 
C# 
private async void DataGrid_Loaded(object sender, RoutedEventArgs e) 
        {           
            var collection = (this.dataGrid.View as PagedCollectionView).GetInternalList(); 
            int i = 0; 
            foreach (var rec in collection) 
            { 
                var value = (rec as OrderInfo).RecordID; 
 
                if ((int)value == 1119) 
                { 
                    int index = i / this.sfDataPager.PageSize; 
                    this.sfDataPager.PageIndex = index; 
                    this.dataGrid.SelectedIndex = i % this.sfDataPager.PageSize; 
                    var rowindex = this.dataGrid.ResolveToRowIndex(this.dataGrid.SelectedIndex);                   
                    await Task.Delay(5);                  
                        dataGrid.ScrollInView(new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex(rowindex, 1));                                                                                                                    
                    break; 
                } 
                i++; 
            } 
        } 
        private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            this.Frame.Navigate(typeof(MainPage)); 
        } 
 
Please download sample from the below location. 
 
 
 
Regards, 
Srinivasan 
 



WA Will Autio May 5, 2016 04:03 PM UTC

Hi Srinivasan,
Thanks for your reply. It sounds like it should work - but unfortunately it does not.
In the constructor, I added the line:
this.dataGrid.Loaded += dataGrid_Loaded;
And then created a method:
private void dataGrid_Loaded(object sender, RoutedEventArgs e)
Then I added your code (with appropriate changes). When it gets to the line:
 var collection = (this.dataGrid.View asPagedCollectionView).GetInternalList(); 
the .View is still null and throws the following exception:

System.NullReferenceException: Object reference not set to an instance of an object.
   at myApp.ContentsPage.dataGrid_Loaded(Object sender, RoutedEventArgs e)

I did some more investigating and it looks like there is a race going on. When I navigate to the page it enters OnNavigatedTo (I have set a breakpoint there) and a while later it enters dataGrid_Loaded and throws the exception.
Next I added a breakpoint at the end of OnNavigatedTo where I have a line like:
sfDataPager.Source = myDataList;

What I found is that the code enters OnNavigatedTo and then enters dataGrid_Loaded and some time later reaches the end of OnNavigatedTo where it gets to assign an object to sfPager.Source.
So, I thought maybe dataGrid_Loaded is not ready yet. I put in a 
await Task.Delay(25000);
near the start of dataGrid_Loaded to see what would happen. Good news - no exception! .View has a value and the code processes and puts me where I want to be.

My question is "How can I tell dataGrid_Loaded to wait until OnNavigatedTo is finished?"
Or, should I add the code from  dataGrid_Loaded to the end of OnNavigatedTo?
What it looks like I need to do is find a way to wait until OnNavigatedTo is completed and dataGrid is Loaded. 
Or change when I do the calculations that are in OnNavigatedTo.

I welcome your recommendations.
Regards,
Will


SV Srinivasan Vasu Syncfusion Team May 9, 2016 12:53 AM UTC

Hi Will, 
 
We have analyzed your query. We are unable to reproduce the reported “System.NullReferenceException: Object reference not set to an instance of an object” issue. 
 
In the SfDataGrid.Loaded event, the View has been created without any task delay. Could you please refer the attached sample and let us know whether you are using the same. Otherwise please modify the sample as per your application, to replicate the issue. 
 
Please refer the below code example. 
 
C# 
   protected override void OnNavigatedTo(NavigationEventArgs e) 
        { 
            ViewModel vm = new ViewModel(); 
            sfDataPager.Source = vm.OrderList;                     
            this.dataGrid.Loaded += DataGrid_Loaded; 
        } 
 
Please download sample from the below location. 
 
 
Regards,
Srinivasan 
 


Loader.
Live Chat Icon For mobile
Up arrow icon