Creating DataGrid and get data from the SQLite database using EF core 6 and MVVM pattern

Hi,

I want to get data from the SQLite database. I'm loading a large amount of data so I need to have pagination. I used the Code-Behind approach to load chunks of data based on pagination. The code is:

View Code_Behind:

public partial class EquipmentIdentity : UserControl
{
public EquipmentIdentity()
{
InitializeComponent();
dataPager.OnDemandLoading += dataPager_OnDemandLoading;
EquipmentIdentityViewModel eivm = new EquipmentIdentityViewModel();
DataContext = eivm;


}


private void dataPager_OnDemandLoading(object sender, Syncfusion.UI.Xaml.Controls.DataPager.OnDemandLoadingEventArgs args)
{
List data;
using (var _sqliteContext = new SQLiteContext())
{
data = _sqliteContext.Equipments.Include(x => x.CostCenter).Skip(args.StartIndex).Take(args.PageSize).ToList();
}
dataPager.LoadDynamicItems(args.StartIndex, data);
//resetting cache for all pages.
(dataPager.PagedSource as PagedCollectionView).ResetCache();
}
}

How can I use MVVM for pagination?


3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team July 18, 2022 06:01 PM UTC

Hi Fardin,

Your requirement to load data by using MVVM for pagination in SfDataPager can be achieved by using the XAML behavior. Please refer to the below code snipet,

XAML Code Snippet:

xmlns:interactivity=http://schemas.microsoft.com/xaml/behaviors

 

<syncfusion:SfDataPager x:Name="dataPager"

                                Grid.Row="1"

                                PageCount="5"

                                PageSize="10"

                                UseOnDemandPaging="True"

                                DisplayMode ="FirstLastPreviousNextNumeric"

                                AutoEllipsisMode="Both"

                                Height="50"

                                AccentBackground="#FF53C3D5"

                                AccentForeground="White"

                                NumericButtonCount="5" >

            <interactivity:Interaction.Behaviors>

                <local:SfDataPagerBehavior />

            </interactivity:Interaction.Behaviors>

</syncfusion:SfDataPager>



C# code snippet of SfDataPagerBehavior :

public class SfDataPagerBehavior : Behavior<SfDataPager>

{

        Northwind northWind;

        List<Orders> source = new List<Orders>();

 

        protected override void OnAttached()

        {

            base.OnAttached();

            string connectionString = string.Format(@"Data Source = {0}", ("Northwind.sdf"));

            northWind = new Northwind(connectionString);

            source = northWind.Orders.ToList();

            this.AssociatedObject.OnDemandLoading += OnOnDemandLoading;

        }

 

        private void OnOnDemandLoading(object sender, OnDemandLoadingEventArgs e)

        {           

            AssociatedObject.LoadDynamicItems(e.StartIndex, source.Skip(e.StartIndex).Take(e.PageSize));

        }

 

        protected override void OnDetaching()

        {

            base.OnDetaching();

           

        }

}


Note: You should install Microsoft.Xaml.Behaviors.Wpf NuGet package to using XAML Behaviors for WPF in application

UG Link: https://help.syncfusion.com/wpf/datapager/getting-started

https://help.syncfusion.com/wpf/datagrid/paging

Please find the sample in the attachment and let us know if you have any concerns in this.


Regards,

Vijayarasan S


Attachment: SfDataPager_4caca00.zip

Marked as answer

FA Fardin replied to Vijayarasan Sivanandham July 20, 2022 06:06 AM UTC

Thanks. It works well.



VS Vijayarasan Sivanandham Syncfusion Team July 20, 2022 02:03 PM UTC

Hi Fardin,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.


Regards,

Vijayarasan S


Loader.
Up arrow icon