dataPager.OnDemandLoading += dataPager_OnDemandLoading; private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args) { //Finding the Page Index int pgIndex = (args.StartIndex + 1) / dataPager.PageSize; //Getting the records for page based on EmployeeID and PageIndex. So each page will show records for particular EmployeeID. var pagesource = source.Where(ord => ord.EmployeeID == pgIndex + 1); //Loading data for page dataPager.LoadDynamicItems(args.StartIndex, pagesource); } |
OnDemandPaging
instead of loading data for all pages. For more details, you can refer below documentation link, <syncfusion:SfDataGrid x:Name="dataGrid" AllowFiltering="True" AllowResizingColumns="True" AutoGenerateColumns="False" AllowSorting="True" ItemsSource="{Binding Path=PagedSource, ElementName=sfDataPager}" NavigationMode="Row" RowHeight="24.5"> <interactivity:Interaction.Behaviors> <local:OnDemandLoadBehavior /> </interactivity:Interaction.Behaviors> |
public class OnDemandLoadBehavior : Behavior<MainWindow> { private EmployeeInfoRespository repository; private List<Employees> source; protected override void OnAttached() { AssociatedObject.Loaded += OnAssociateObjectLoad; repository = new EmployeeInfoRespository(); source = repository.GetEmployeesDetails_List(2000); } void OnAssociateObjectLoad(object sender, System.Windows.RoutedEventArgs e) { AssociatedObject.sfDataPager.OnDemandLoading += OnDemandLoading; AssociatedObject.sfDataPager.MoveToFirstPage(); } void OnDemandLoading(object sender, OnDemandLoadingEventArgs args) { AssociatedObject.sfDataPager.LoadDynamicItems(args.StartIndex,source.Skip(args.StartIndex).Take(args.PageSize)); } protected override void OnDetaching() { AssociatedObject.Loaded -= OnAssociateObjectLoad; AssociatedObject.sfDataPager.OnDemandLoading -= OnDemandLoading; } } |
This is Ok but, now I want to join this example with filtering and to have datagrid with datapager and filtering with MVVM.I've attached a .rar, this rar is based in filtering_demo, but I have modified the code (VS 2015), but it is not working, with the helper behavior public class OnDemandLoadBehavior : Behavior I can load the datapager on demanding but I don´t know how to join it with the filtering
protected override void OnAttached() { viewmodel = AssociatedObject.DataContext as EmployeeInfoViewModel; AssociatedObject.Loaded += OnAssociateObjectLoad; (AssociatedObject.DataContext as EmployeeInfoViewModel).FilterChanged += OnFilterChanged; empsource = viewmodel.EmployeeDetails; } IEnumerable<Employees> empsource; private void OnFilterChanged() { empsource = viewmodel.EmployeeDetails.Where(emp => viewmodel.FilerRecords(emp)); var totalcount = empsource.Count(); var pagecount = 0; if (totalcount < this.AssociatedObject.SfDataPager.PageSize) pagecount = 1; else pagecount = totalcount / this.AssociatedObject.SfDataPager.PageSize; if (this.AssociatedObject.SfDataPager.PageCount != pagecount) this.AssociatedObject.SfDataPager.PageCount = pagecount; (this.AssociatedObject.sfGrid.View as PagedCollectionView).ResetCache(); (this.AssociatedObject.sfGrid.View as PagedCollectionView).ResetCacheForPage(this.AssociatedObject.SfDataPager.PageIndex); (this.AssociatedObject.sfGrid.View as PagedCollectionView).MoveToFirstPage(); } |