|
1 | | -# How to change the PageCount at runtime when data loaded on demand is filtered in winforms datapager? |
2 | | -This example illustrates how to change the PageCount at runtime when data loaded on demand is filtered in winforms datapager |
| 1 | +# How to change the PageCount at runtime when data loaded on demand is filtered in winforms datapager |
| 2 | + |
| 3 | +This example illustrates how to change the `PageCount` at runtime when data loaded on demand is filtered in [WinForms DataPager](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataPager.SfDataPager.html). |
| 4 | + |
| 5 | +You can change the [SfDataPager.PageCount](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.DataPager.SfDataPager.html#Syncfusion_WinForms_DataPager_SfDataPager_PageCount) at runtime based on the records count in on-demand paging. Here, `PageCount` are modified by filtering the records in run time. |
| 6 | + |
| 7 | +```c# |
| 8 | +public partial class Form1 : Form |
| 9 | +{ |
| 10 | + Northwind northWind; |
| 11 | + List<Orders> source = new List<Orders>(); |
| 12 | + |
| 13 | + public Form1() |
| 14 | + { |
| 15 | + InitializeComponent(); |
| 16 | + string connectionString = string.Format(@"Data Source = {0}", ("Northwind.sdf")); |
| 17 | + //northWind dataProvider connectivity. |
| 18 | + northWind = new Northwind(connectionString); |
| 19 | + source = northWind.Orders.ToList(); |
| 20 | + this.sfDataPager1.OnDemandLoading += OnDemandLoading; |
| 21 | + } |
| 22 | + |
| 23 | + private void OnDemandLoading(object sender, OnDemandLoadingEventArgs e) |
| 24 | + { |
| 25 | + sfDataPager1.LoadDynamicData(e.StartRowIndex, source.Skip(e.StartRowIndex).Take(e.PageSize)); |
| 26 | + } |
| 27 | + |
| 28 | + private List<Orders> ApplyFilter(Northwind NorthwindSource) |
| 29 | + { |
| 30 | + // records are filtered based on CustomerID column |
| 31 | + return NorthwindSource.Orders.Where(item => item.CustomerID.Contains(filterTextBox.Text)).ToList(); |
| 32 | + } |
| 33 | + |
| 34 | + private void FilterBtn_Click(object sender, System.EventArgs e) |
| 35 | + { |
| 36 | + source = ApplyFilter(northWind); |
| 37 | + //page count resets based on filtered records. |
| 38 | + if (source.Count() < sfDataPager1.PageSize) |
| 39 | + this.sfDataPager1.PageCount = 1; |
| 40 | + else |
| 41 | + { |
| 42 | + var count = source.Count() / sfDataPager1.PageSize; |
| 43 | + if (source.Count() % sfDataPager1.PageSize == 0) |
| 44 | + this.sfDataPager1.PageCount = count; |
| 45 | + else |
| 46 | + this.sfDataPager1.PageCount = count + 1; |
| 47 | + } |
| 48 | + this.sfDataPager1.MoveToPage(0); |
| 49 | + this.sfDataPager1.Refresh(); |
| 50 | + } |
| 51 | +``` |
0 commit comments