@@ -1386,91 +1386,88 @@ public class OrderDetails
13861386
13871387### AutoFit on Column Visibility Change
13881388
1389- The Syncfusion <sup style = " font-size:70%" > & reg ;< / sup > Blazor DataGrid can automatically adjust column widths when column visibility changes through the column chooser . This behavior is implemented by invoking the [AutoFitColumnsAsync ](https :// help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_AutoFitColumnsAsync) method in the [OnActionComplete ](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid -1.html) event. Use the **RequestType** property in the event arguments to identify the action and call `AutoFitColumnsAsync` when the request type is **ColumnState** .
1389+ The Syncfusion <sup style = " font-size:70%" > & reg ;< / sup > Blazor DataGrid can automatically adjust column widths when column visibility changes through the column chooser . This behavior is implemented by invoking [AutoFitColumnsAsync ](https :// help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.SfGrid-1.html#Syncfusion_Blazor_Grids_SfGrid_1_AutoFitColumnsAsync) from the [ColumnVisibilityChanged ](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridEvents -1.html#Syncfusion_Blazor_Grids_GridEvents_1_ColumnVisibilityChanged ) event, which is raised after the column chooser applies visibility updates. The [ColumnVisibilityChangedEventArgs](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ColumnVisibilityChangedEventArgs.html) class provides context for the columns that became visible or hidden .
13901390
13911391The example demonstrates autofitting columns after changing visibility using the [Column Chooser ](https :// blazor.syncfusion.com/documentation/datagrid/column-chooser):
13921392
13931393{% tabs % }
13941394{% highlight razor tabtitle = " Index.razor" % }
1395- @using Syncfusion .Blazor .Grids ;
13961395
1397- < SfGrid @ref = " Grid" DataSource = " @Orders" ShowColumnChooser = " true" Toolbar = @ToolbarItems >
1398- < GridEvents OnActionComplete = " OnActionComplete" TValue = " OrderData" >< / GridEvents >
1396+ @using Syncfusion .Blazor .Grids
1397+
1398+ < SfGrid @ref = " Grid" DataSource = " @Orders" ShowColumnChooser = " true" Toolbar = " @ToolbarItems" >
1399+ < GridEvents ColumnVisibilityChanged = " OnColumnVisibilityChanged" TValue = " OrderData" >< / GridEvents >
13991400 < GridColumns >
1400- < GridColumn Field = @nameof (OrderData .OrderID ) TextAlign = " TextAlign.Center " HeaderText = " Order ID" Width = " 120" >< / GridColumn >
1401+ < GridColumn Field = @nameof (OrderData .OrderID ) HeaderText = " Order ID" TextAlign = " TextAlign.Center " Width = " 120" >< / GridColumn >
14011402 < GridColumn Field = @nameof (OrderData .CustomerID ) HeaderText = " Customer ID" Width = " 130" >< / GridColumn >
14021403 < GridColumn Field = @nameof (OrderData .ShipName ) HeaderText = " Ship Name" Width = " 130" >< / GridColumn >
14031404 < GridColumn Field = @nameof (OrderData .ShipAddress ) HeaderText = " Ship Address" Width = " 120" >< / GridColumn >
1404- < GridColumn Field = @nameof (OrderData .ShipCity ) HeaderText = " Ship City" Format = " d " TextAlign = " TextAlign.Right" Width = " 150" >< / GridColumn >
1405+ < GridColumn Field = @nameof (OrderData .ShipCity ) HeaderText = " Ship City" TextAlign = " TextAlign.Right" Width = " 150" >< / GridColumn >
14051406 < / GridColumns >
14061407< / SfGrid >
14071408
1408- @code {
1409- private SfGrid < OrderData > Grid ;
1410- public List < OrderData > Orders { get ; set ; }
1411- public string [] ToolbarItems = new string [] { " ColumnChooser" };
1412-
1409+ @code
1410+ {
1411+ private SfGrid < OrderData > Grid { get ; set ; }
1412+ public List < OrderData > Orders { get ; private set ; } = new ();
1413+ public string [] ToolbarItems { get ; } = new [] { " ColumnChooser" };
1414+
14131415 protected override void OnInitialized ()
14141416 {
14151417 Orders = OrderData .GetAllRecords ();
1416- }
1417- public async Task OnActionComplete (ActionEventArgs < OrderData > Args )
1418+ }
1419+
1420+ public async Task OnColumnVisibilityChanged (ColumnVisibilityChangedEventArgs args )
14181421 {
1419- if (Args .RequestType == Syncfusion .Blazor .Grids .Action .ColumnState )
1420- {
1421- await Grid .AutoFitColumnsAsync ();
1422- }
1422+ await Grid .AutoFitColumnsAsync ();
14231423 }
14241424}
1425+
14251426{% endhighlight % }
14261427{% highlight c # tabtitle = " OrderData.cs" % }
1427- public class OrderData
1428+
1429+ public class OrderData
1430+ {
1431+ public int ? OrderID { get ; set ; }
1432+ public string CustomerID { get ; set ; }
1433+ public string ShipName { get ; set ; }
1434+ public string ShipAddress { get ; set ; }
1435+ public string ShipCity { get ; set ; }
1436+
1437+ private static readonly List < OrderData > Orders = new ();
1438+
1439+ public OrderData (int ? orderID , string customerID , string shipName , string shipAddress , string shipCity )
14281440 {
1429- public static List < OrderData > Orders = new List <OrderData >();
1430- public OrderData ()
1431- {
1441+ OrderID = orderID ;
1442+ CustomerID = customerID ;
1443+ ShipName = shipName ;
1444+ ShipAddress = shipAddress ;
1445+ ShipCity = shipCity ;
1446+ }
14321447
1433- }
1434- public OrderData (int ? OrderID , string CustomerID ,string ShipName , string ShipAddress , string ShipCity )
1435- {
1436- this .OrderID = OrderID ;
1437- this .CustomerID = CustomerID ;
1438- this .ShipName = ShipName ;
1439- this .ShipAddress = ShipAddress ;
1440- this .ShipCity = ShipCity ;
1441- }
1442- public static List < OrderData > GetAllRecords ()
1448+ public static List < OrderData > GetAllRecords ()
1449+ {
1450+ if (! Orders .Any ())
14431451 {
1444- if (Orders .Count () == 0 )
1445- {
1446- int code = 10 ;
1447- for (int i = 1 ; i < 2 ; i ++ )
1448- {
1449- Orders .Add (new OrderData (10248 , " VINET" , " Vins et alcools Chevalier" , " 2, rue du Commerce" , " Reims" ));
1450- Orders .Add (new OrderData (10249 , " TOMSP" , " Toms Spezialitäten" , " Boulevard Tirou, 255" , " Charleroi" ));
1451- Orders .Add (new OrderData (10250 , " HANAR" , " Hanari Carnes" , " Rua do Paço, 67" , " Rio de Janeiro" ));
1452- Orders .Add (new OrderData (10251 , " VINET" , " Victuailles en stock" , " Hauptstr. 31" , " Bern" ));
1453- Orders .Add (new OrderData (10252 , " SUPRD" , " Suprêmes délices" , " Starenweg 5" , " Genève" ));
1454- Orders .Add (new OrderData (10253 , " HANAR" , " Hanari Carnes" , " Rua do Mercado, 12" , " Resende" ));
1455- Orders .Add (new OrderData (10254 , " CHOPS" , " Chop-suey Chinese" , " Carrera 22 con Ave. Carlos Soublette #8-35" , " San Cristóbal" ));
1456- Orders .Add (new OrderData (10255 , " VINET" , " Richter Supermarkt" , " Kirchgasse 6" , " Graz" ));
1457- Orders .Add (new OrderData (10256 , " HANAR" , " Wellington Importadora" , " Sierras de Granada 9993" , " México D.F." ));
1458- code += 5 ;
1459- }
1460- }
1461- return Orders ;
1452+ Orders .Add (new OrderData (10248 , " VINET" , " Vins et alcools Chevalier" , " 2, rue du Commerce" , " Reims" ));
1453+ Orders .Add (new OrderData (10249 , " TOMSP" , " Toms Spezialitäten" , " Boulevard Tirou, 255" , " Charleroi" ));
1454+ Orders .Add (new OrderData (10250 , " HANAR" , " Hanari Carnes" , " Rua do Paço, 67" , " Rio de Janeiro" ));
1455+ Orders .Add (new OrderData (10251 , " VINET" , " Victuailles en stock" , " Hauptstr. 31" , " Bern" ));
1456+ Orders .Add (new OrderData (10252 , " SUPRD" , " Suprêmes délices" , " Starenweg 5" , " Genève" ));
1457+ Orders .Add (new OrderData (10253 , " HANAR" , " Hanari Carnes" , " Rua do Mercado, 12" , " Resende" ));
1458+ Orders .Add (new OrderData (10254 , " CHOPS" , " Chop-suey Chinese" , " Carrera 22 con Ave. Carlos Soublette #8-35" , " San Cristóbal" ));
1459+ Orders .Add (new OrderData (10255 , " VINET" , " Richter Supermarkt" , " Kirchgasse 6" , " Graz" ));
1460+ Orders .Add (new OrderData (10256 , " HANAR" , " Wellington Importadora" , " Sierras de Granada 9993" , " México D.F." ));
14621461 }
14631462
1464- public int ? OrderID { get ; set ; }
1465- public string CustomerID { get ; set ; }
1466- public string ShipName { get ; set ; }
1467- public string ShipCity { get ; set ; }
1468- public string ShipAddress { get ; set ; }
1469- }
1463+ return Orders ;
1464+ }
1465+ }
1466+
14701467{% endhighlight % }
14711468{% endtabs % }
14721469
1473- {% previewsample " https://blazorplayground.syncfusion.com/embed/LtrUWCZmBqLMLrUr ?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" % }
1470+ {% previewsample " https://blazorplayground.syncfusion.com/embed/rNLICrNzRDbCTmVz ?appbar=false&editor=false&result=true&errorlist=false&theme=bootstrap5" % }
14741471
14751472## Fixed columns
14761473
0 commit comments