Skip to content

Commit 85108d4

Browse files
Merge pull request #1 from SyncfusionExamples/Auto_Scrolling
MAUI- 965930 - Prepared KB Sample for add row with auto scrolling in SfDataGrid
2 parents ffd737c + 82ec397 commit 85108d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+9256
-2
lines changed

README.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,69 @@
1-
# How-to-add-a-row-with-auto-scrolling-behavior-in-.NET-MAUI-DataGrid-SfDataGrid
2-
This demo shows how to add a row with auto scrolling behavior in .NET MAUI DataGrid (SfDataGrid)?
1+
# How to add a row with auto scrolling behavior in .NET MAUI DataGrid SfDataGrid
2+
This demo shows how to add a row with auto scrolling behavior in the Syncfusion [.NET MAUI DataGrid](https://help.syncfusion.com/maui/datagrid/overview) `SfDataGrid`.
3+
4+
This sample helps you to to auto scroll the DataGrid to the last item added in the collection. In this sample, after adding the new data to the grid, the scrolling was performed automatically.
5+
6+
## MainPage.xaml
7+
```
8+
<ContentPage.BindingContext>
9+
<local:OrderInfoRepository x:Name="viewModel" />
10+
</ContentPage.BindingContext>
11+
12+
<Grid RowDefinitions="50,*">
13+
<Button Text="AddRow" WidthRequest="200"
14+
Clicked="Button_Clicked" />
15+
16+
<syncfusion:SfDataGrid x:Name="dataGrid" Grid.Row="1"
17+
ColumnWidthMode="Fill"
18+
ItemsSource="{Binding OrderInfoCollection}">
19+
20+
</syncfusion:SfDataGrid>
21+
</Grid>
22+
```
23+
24+
## MainPage.xaml.cs
25+
```
26+
public partial class MainPage : ContentPage
27+
{
28+
public MainPage()
29+
{
30+
InitializeComponent();
31+
dataGrid.Loaded += DataGrid_Loaded;
32+
}
33+
34+
private void DataGrid_Loaded(object? sender, EventArgs e)
35+
{
36+
dataGrid.View!.CollectionChanged += MainPage_CollectionChanged;
37+
}
38+
39+
private async void MainPage_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
40+
{
41+
if (e.Action == NotifyCollectionChangedAction.Add)
42+
{
43+
await Task.Delay(500);
44+
await dataGrid.ScrollToRowIndex(viewModel.OrderInfoCollection.Count - 1);
45+
}
46+
}
47+
48+
private void Button_Clicked(object sender, EventArgs e)
49+
{
50+
OrderInfo order = new OrderInfo("2001", "Andrew Fuller", "France", "BLONP", "Strasbourg");
51+
viewModel.OrderInfoCollection.Add(order);
52+
}
53+
}
54+
```
55+
56+
57+
Executing the code example above yields the following output
58+
59+
<img src="https://github.com/user-attachments/assets/14be5823-e5c8-44af-8c9d-0ba973de9856" />
60+
61+
62+
Take a moment to explore this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
63+
64+
### Conclusion
65+
I hope you enjoyed learning about how to add a row with auto scroll in SfDataGrid.
66+
67+
You can refer to our [.NET MAUI DataGrid’s feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to learn about its other groundbreaking feature representations. You can also explore our [.NET MAUI DataGrid Documentation](https://help.syncfusion.com/maui/datagrid/getting-started) to understand how to present and manipulate data. For current customers, you can check out our .NET MAUI components on the [License and Downloads](https://www.syncfusion.com/sales/teamlicense) page. If you are new to Syncfusion, you can try our 30-day [free trial](https://www.syncfusion.com/downloads/maui) to explore our .NET MAUI DataGrid and other .NET MAUI components.
68+
69+
If you have any queries or require clarifications, please let us know in the comments below. You can also contact us through our [support forums](https://www.syncfusion.com/forums),[Direct-Trac](https://support.syncfusion.com/create) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid), or the feedback portal. We are always happy to assist you!
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35825.156 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfDataGridSample", "SfDataGridSample\SfDataGridSample.csproj", "{1C376DAF-3809-446B-996B-371DBB2A563A}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{1C376DAF-3809-446B-996B-371DBB2A563A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1C376DAF-3809-446B-996B-371DBB2A563A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1C376DAF-3809-446B-996B-371DBB2A563A}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{1C376DAF-3809-446B-996B-371DBB2A563A}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{1C376DAF-3809-446B-996B-371DBB2A563A}.Release|Any CPU.Build.0 = Release|Any CPU
19+
EndGlobalSection
20+
GlobalSection(SolutionProperties) = preSolution
21+
HideSolutionNode = FALSE
22+
EndGlobalSection
23+
GlobalSection(ExtensibilityGlobals) = postSolution
24+
SolutionGuid = {2C006871-2D19-4DBA-852B-E726BBB82434}
25+
EndGlobalSection
26+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:SfDataGridSample"
5+
x:Class="SfDataGridSample.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
}
9+
10+
protected override Window CreateWindow(IActivationState? activationState)
11+
{
12+
return new Window(new AppShell());
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="SfDataGridSample.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:SfDataGridSample"
7+
Shell.FlyoutBehavior="Flyout"
8+
Title="SfDataGridSample">
9+
10+
<ShellContent
11+
Title="Home"
12+
ContentTemplate="{DataTemplate local:MainPage}"
13+
Route="MainPage" />
14+
15+
</Shell>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SfDataGridSample
2+
{
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:SfDataGridSample"
5+
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
6+
x:Class="SfDataGridSample.MainPage">
7+
8+
<ContentPage.BindingContext>
9+
<local:OrderInfoRepository x:Name="ViewModel" />
10+
</ContentPage.BindingContext>
11+
12+
<Grid RowDefinitions="50,*">
13+
<Button Text="AddRow" WidthRequest="200"
14+
Clicked="Button_Clicked" />
15+
16+
<syncfusion:SfDataGrid x:Name="dataGrid" Grid.Row="1"
17+
ColumnWidthMode="Fill"
18+
ItemsSource="{Binding OrderInfoCollection}">
19+
20+
</syncfusion:SfDataGrid>
21+
</Grid>
22+
23+
</ContentPage>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Syncfusion.Maui.Core.Carousel;
2+
using System.Collections.Specialized;
3+
4+
namespace SfDataGridSample
5+
{
6+
public partial class MainPage : ContentPage
7+
{
8+
int count = 0;
9+
10+
public MainPage()
11+
{
12+
InitializeComponent();
13+
dataGrid.Loaded += DataGrid_Loaded;
14+
}
15+
16+
private void DataGrid_Loaded(object? sender, EventArgs e)
17+
{
18+
dataGrid.View!.CollectionChanged += MainPage_CollectionChanged;
19+
}
20+
21+
private async void MainPage_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
22+
{
23+
if (e.Action == NotifyCollectionChangedAction.Add)
24+
{
25+
await Task.Delay(500);
26+
await dataGrid.ScrollToRowIndex(ViewModel.OrderInfoCollection.Count - 1);
27+
}
28+
}
29+
30+
private void Button_Clicked(object sender, EventArgs e)
31+
{
32+
OrderInfo order = new OrderInfo("2001", "Andrew Fuller", "France", "BLONP", "Strasbourg");
33+
ViewModel.OrderInfoCollection.Add(order);
34+
}
35+
36+
}
37+
38+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Extensions.Logging;
2+
using Syncfusion.Maui.Core.Hosting;
3+
4+
namespace SfDataGridSample;
5+
6+
public static class MauiProgram
7+
{
8+
public static MauiApp CreateMauiApp()
9+
{
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.UseMauiApp<App>()
13+
.ConfigureSyncfusionCore()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
18+
});
19+
20+
#if DEBUG
21+
builder.Logging.AddDebug();
22+
#endif
23+
24+
return builder.Build();
25+
}
26+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SfDataGridSample
8+
{
9+
public class OrderInfo
10+
{
11+
private string orderID;
12+
private string customerID;
13+
private string customer;
14+
private string shipCity;
15+
private string shipCountry;
16+
17+
public string OrderID
18+
{
19+
get { return orderID; }
20+
set { this.orderID = value; }
21+
}
22+
23+
public string CustomerID
24+
{
25+
get { return customerID; }
26+
set { this.customerID = value; }
27+
}
28+
29+
public string ShipCountry
30+
{
31+
get { return shipCountry; }
32+
set { this.shipCountry = value; }
33+
}
34+
35+
public string Customer
36+
{
37+
get { return this.customer; }
38+
set { this.customer = value; }
39+
}
40+
41+
public string ShipCity
42+
{
43+
get { return shipCity; }
44+
set { this.shipCity = value; }
45+
}
46+
47+
public OrderInfo(string orderId, string customerId, string country, string customer, string shipCity)
48+
{
49+
this.OrderID = orderId;
50+
this.CustomerID = customerId;
51+
this.Customer = customer;
52+
this.ShipCountry = country;
53+
this.ShipCity = shipCity;
54+
}
55+
56+
57+
}
58+
}

0 commit comments

Comments
 (0)