|
1 | 1 | --- |
2 | 2 | layout: post |
3 | 3 | title: Grouping in Blazor DataGrid | Syncfusion |
4 | | -description: Checkout and learn here all about Grouping in Syncfusion Blazor DataGrid and much more details. |
| 4 | +description: Checkout and learn here all about Grouping in Syncfusion Blazor DataGrid component and much more details. |
5 | 5 | platform: Blazor |
6 | 6 | control: DataGrid |
7 | 7 | documentation: ug |
@@ -466,6 +466,109 @@ public class OrderData |
466 | 466 |
|
467 | 467 | {% previewsample "https://blazorplayground.syncfusion.com/embed/rtLqCCBPUOxoyWyr?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %} |
468 | 468 |
|
| 469 | +## Persist grouped row expand or collapse state |
| 470 | + |
| 471 | +The Syncfusion Blazor DataGrid offers the ability to persist the expand or collapse state of grouped rows across various data operations such as paging, sorting, filtering, and editing. By default, these operations reset the grouped rows to their initial collapsed or expanded state. |
| 472 | +To retain the current state of grouped rows and ensure a consistent user experience, set the [GridGroupSettings.PersistGroupState](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridGroupSettings.html#Syncfusion_Blazor_Grids_GridGroupSettings_PersistGroupState) property to **true**. This also applies when using external grouping methods like [ExpandAllGroupAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_ExpandAllGroupAsync) and [CollapseAllGroupAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_CollapseAllGroupAsync). |
| 473 | + |
| 474 | +The following example demonstrates how to dynamically enable or disable the `PersistGroupState` property in the DataGrid. |
| 475 | + |
| 476 | +{% tabs %} |
| 477 | +{% highlight razor tabtitle="Index.razor" %} |
| 478 | + |
| 479 | +@using Syncfusion.Blazor.Grids |
| 480 | +@using Syncfusion.Blazor.Buttons |
| 481 | + |
| 482 | +<div style="display:flex;gap: 5px;"> |
| 483 | + <label> Enable or disable grouped row state persistence</label> |
| 484 | + <SfSwitch OffLabel="OFF" OnLabel="ON" ValueChange="Change" TChecked="bool?"></SfSwitch> |
| 485 | +</div> |
| 486 | + |
| 487 | +<SfGrid @ref="Grid" DataSource="@GridData" AllowGrouping="true" Height="315px" AllowSorting="true"> |
| 488 | + <GridGroupSettings Columns="@Initial" PersistGroupState=@IsPersist></GridGroupSettings> |
| 489 | + <GridColumns> |
| 490 | + <GridColumn Field=@nameof(OrderData.OrderID) HeaderText="Order ID" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right" Width="90"></GridColumn> |
| 491 | + <GridColumn Field=@nameof(OrderData.CustomerID) HeaderText="Customer ID" Width="100"></GridColumn> |
| 492 | + <GridColumn Field=@nameof(OrderData.ShipCity) HeaderText="Ship City" Width="100"></GridColumn> |
| 493 | + <GridColumn Field=@nameof(OrderData.ShipName) HeaderText="Ship Name" Width="120"></GridColumn> |
| 494 | + </GridColumns> |
| 495 | +</SfGrid> |
| 496 | + |
| 497 | +@code { |
| 498 | + public List<OrderData> GridData { get; set; } |
| 499 | + SfGrid<OrderData>? Grid { get; set; } |
| 500 | + |
| 501 | + private bool IsPersist { get; set; } = true; |
| 502 | + private string[] Initial = (new string[] { "CustomerID", "ShipCity" }); |
| 503 | + |
| 504 | + protected override void OnInitialized() |
| 505 | + { |
| 506 | + GridData = OrderData.GetAllRecords(); |
| 507 | + } |
| 508 | + |
| 509 | + private async Task Change(Syncfusion.Blazor.Buttons.ChangeEventArgs<bool?> args) |
| 510 | + { |
| 511 | + if (args.Checked == true) |
| 512 | + { |
| 513 | + IsPersist = true; |
| 514 | + } |
| 515 | + else |
| 516 | + { |
| 517 | + IsPersist = false; |
| 518 | + } |
| 519 | + } |
| 520 | +} |
| 521 | + |
| 522 | +{% endhighlight %} |
| 523 | + |
| 524 | +{% highlight c# tabtitle="OrderData.cs" %} |
| 525 | + |
| 526 | +public class OrderData |
| 527 | +{ |
| 528 | + public static List<OrderData> Orders = new List<OrderData>(); |
| 529 | + public OrderData(){} |
| 530 | + |
| 531 | + public OrderData(int? OrderID, string CustomerID, string ShipCity, string ShipName) |
| 532 | + { |
| 533 | + this.OrderID = OrderID; |
| 534 | + this.CustomerID = CustomerID; |
| 535 | + this.ShipCity = ShipCity; |
| 536 | + this.ShipName = ShipName; |
| 537 | + } |
| 538 | + |
| 539 | + public static List<OrderData> GetAllRecords() |
| 540 | + { |
| 541 | + if (Orders.Count() == 0) |
| 542 | + { |
| 543 | + int code = 10; |
| 544 | + for (int i = 1; i < 2; i++) |
| 545 | + { |
| 546 | + Orders.Add(new OrderData(10248, "VINET", "Reims", "Vins et alcools Chevali")); |
| 547 | + Orders.Add(new OrderData(10249, "TOMSP", "Münster", "Toms Spezialitäten")); |
| 548 | + Orders.Add(new OrderData(10250, "HANAR", "Rio de Janeiro", "Hanari Carnes")); |
| 549 | + Orders.Add(new OrderData(10251, "VICTE", "Lyon", "Victuailles en stock")); |
| 550 | + Orders.Add(new OrderData(10252, "SUPRD", "Charleroi", "Suprêmes délices")); |
| 551 | + Orders.Add(new OrderData(10253, "HANAR", "Lyon", "Hanari Carnes")); |
| 552 | + Orders.Add(new OrderData(10254, "CHOPS", "Rio de Janeiro", "Chop-suey Chinese")); |
| 553 | + Orders.Add(new OrderData(10255, "RICSU", "Münster", "Richter Supermarkt")); |
| 554 | + Orders.Add(new OrderData(10256, "WELLI", "Reims", "Wellington Import")); |
| 555 | + code += 5; |
| 556 | + } |
| 557 | + } |
| 558 | + return Orders; |
| 559 | + } |
| 560 | + |
| 561 | + public int? OrderID { get; set; } |
| 562 | + public string CustomerID { get; set; } |
| 563 | + public string ShipCity { get; set; } |
| 564 | + public string ShipName { get; set; } |
| 565 | +} |
| 566 | + |
| 567 | +{% endhighlight %} |
| 568 | +{% endtabs %} |
| 569 | + |
| 570 | +{% previewsample "https://blazorplayground.syncfusion.com/embed/hNroDvrRpDAvxclV?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" %} |
| 571 | + |
469 | 572 | ## Sort grouped columns in descending order during initial grouping |
470 | 573 |
|
471 | 574 | By default, grouped columns are sorted in ascending order. However, you can sort them in descending order during initial grouping by setting the [Field](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridSortColumn.html#Syncfusion_Blazor_Grids_GridSortColumn_Field) and [Direction](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridSortColumn.html#Syncfusion_Blazor_Grids_GridSortColumn_Direction) in the `GridSortSettings` of the [Columns](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridSortSettings.html#Syncfusion_Blazor_Grids_GridSortSettings_Columns) property. |
|
0 commit comments