|
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 | + |
| 9 | +```c# |
| 10 | +public partial class Form1 : Form |
| 11 | +{ |
| 12 | + Northwind northWind; |
| 13 | + List<Orders> source = new List<Orders>(); |
| 14 | + |
| 15 | + public Form1() |
| 16 | + { |
| 17 | + InitializeComponent(); |
| 18 | + string connectionString = string.Format(@"Data Source = {0}", ("Northwind.sdf")); |
| 19 | + //northWind dataProvider connectivity. |
| 20 | + northWind = new Northwind(connectionString); |
| 21 | + source = northWind.Orders.ToList(); |
| 22 | + this.sfDataPager1.OnDemandLoading += OnDemandLoading; |
| 23 | + } |
| 24 | + |
| 25 | + private void OnDemandLoading(object sender, OnDemandLoadingEventArgs e) |
| 26 | + { |
| 27 | + sfDataPager1.LoadDynamicData(e.StartRowIndex, source.Skip(e.StartRowIndex).Take(e.PageSize)); |
| 28 | + } |
| 29 | + |
| 30 | + private List<Orders> ApplyFilter(Northwind NorthwindSource) |
| 31 | + { |
| 32 | + // records are filtered based on CustomerID column |
| 33 | + return NorthwindSource.Orders.Where(item => item.CustomerID.Contains(filterTextBox.Text)).ToList(); |
| 34 | + } |
| 35 | + |
| 36 | + private void FilterBtn_Click(object sender, System.EventArgs e) |
| 37 | + { |
| 38 | + source = ApplyFilter(northWind); |
| 39 | + //page count resets based on filtered records. |
| 40 | + if (source.Count() < sfDataPager1.PageSize) |
| 41 | + this.sfDataPager1.PageCount = 1; |
| 42 | + else |
| 43 | + { |
| 44 | + var count = source.Count() / sfDataPager1.PageSize; |
| 45 | + if (source.Count() % sfDataPager1.PageSize == 0) |
| 46 | + this.sfDataPager1.PageCount = count; |
| 47 | + else |
| 48 | + this.sfDataPager1.PageCount = count + 1; |
| 49 | + } |
| 50 | + this.sfDataPager1.MoveToPage(0); |
| 51 | + this.sfDataPager1.Refresh(); |
| 52 | + } |
| 53 | +``` |
| 54 | + |
| 55 | +#### VB |
| 56 | + |
| 57 | +``` vb |
| 58 | +Partial Public Class Form1 |
| 59 | + Inherits Form |
| 60 | +Private northWind As Northwind |
| 61 | +Private source As New List(Of Orders)() |
| 62 | + |
| 63 | +Public Sub New() |
| 64 | + InitializeComponent() |
| 65 | + Dim connectionString As String = String.Format("Data Source = {0}", ("Northwind.sdf")) |
| 66 | + 'northWind dataProvider connectivity. |
| 67 | + northWind = New Northwind(connectionString) |
| 68 | + source = northWind.Orders.ToList() |
| 69 | + AddHandler Me.sfDataPager1.OnDemandLoading, AddressOf OnDemandLoading |
| 70 | +End Sub |
| 71 | + |
| 72 | +Private Sub OnDemandLoading(ByVal sender As Object, ByVal e As OnDemandLoadingEventArgs) |
| 73 | + sfDataPager1.LoadDynamicData(e.StartRowIndex, source.Skip(e.StartRowIndex).Take(e.PageSize)) |
| 74 | +End Sub |
| 75 | + |
| 76 | +Private Function ApplyFilter(ByVal NorthwindSource As Northwind) As List(Of Orders) |
| 77 | + 'records are filtered based on CustomerID column |
| 78 | + Return NorthwindSource.Orders.Where(Function(item) item.CustomerID.Contains(filterTextBox.Text)).ToList() |
| 79 | +End Function |
| 80 | + |
| 81 | +Private Sub FilterBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) |
| 82 | + source = ApplyFilter(northWind) |
| 83 | + 'page count resets based on filtered records. |
| 84 | + If source.Count() < sfDataPager1.PageSize Then |
| 85 | + Me.sfDataPager1.PageCount = 1 |
| 86 | + Else |
| 87 | + Dim count = source.Count() / sfDataPager1.PageSize |
| 88 | + If source.Count() Mod sfDataPager1.PageSize = 0 Then |
| 89 | + Me.sfDataPager1.PageCount = count |
| 90 | + Else |
| 91 | + Me.sfDataPager1.PageCount = count + 1 |
| 92 | + End If |
| 93 | + End If |
| 94 | + Me.sfDataPager1.MoveToPage(0) |
| 95 | + Me.sfDataPager1.Refresh() |
| 96 | +End Sub |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +Here, records are filtered based on the textbox text in clicking event of Filter button. Initially PageCount is 5 and it is changed as 1 once the records are filtered. |
0 commit comments