Skip to content

Commit 712075a

Browse files
Merge pull request #7224 from syncfusion-content/983330-How-To1
983330: Updated the UG for How-To folder Content and Samples in DataGrid
2 parents fa6eb9f + ab04880 commit 712075a

9 files changed

+216
-186
lines changed

blazor/datagrid/how-to/blazor-webassembly-data-grid-using-cli.md

Lines changed: 76 additions & 54 deletions
Large diffs are not rendered by default.

blazor/datagrid/how-to/create-custom-grid-component.md

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
---
22
layout: post
3-
title: Create custom Grid component in Blazor DataGrid Component | Syncfusion
4-
description: Checkout and learn here all about Create custom Grid component in Syncfusion Blazor DataGrid component and more.
3+
title: Create a custom Grid component in Blazor DataGrid | Syncfusion
4+
description: Learn how to build a reusable custom Grid component that wraps SfGrid in Blazor to share default paging, sorting, and layout settings across the application.
55
platform: Blazor
66
control: DataGrid
77
documentation: ug
88
---
99

1010
# Create custom Blazor DataGrid
1111

12-
You can create a custom DataGrid by rendering the `SfGrid` as a new razor component. It helps to create your own custom component when you want to create multiple Grids with same configuration or with default configuration throughout your application.
12+
Create a reusable custom Grid component by wrapping the `SfGrid` in a Razor component. This approach helps apply consistent defaults (for example, paging and sorting) and common settings across multiple Grids without repeating configuration.
1313

14-
This is demonstrated in the following example by creating a custom Grid called CustomGrid, where `SfGrid` is rendered with some basic default properties such as `GridPageSettings` etc., which will be reflected in all the Grids rendered using the CustomGrid:
14+
The following example creates a `CustomGrid` wrapper that renders `SfGrid` with default properties such as `GridPageSettings`. Any unmatched attributes passed to `CustomGrid` are forwarded to the inner `SfGrid`, and content placed inside `CustomGrid` is projected as columns via `ChildContent`.
1515

1616
CustomGrid.razor
1717

1818
```cshtml
1919
@using Syncfusion.Blazor.Grids
20-
@typeparam TValue
21-
@inherits SfGrid<TValue>
20+
@typeparam TItem
2221
23-
<SfGrid TValue="TValue" AllowSorting="AllowSorting" AllowPaging="AllowPaging" @attributes="props">
22+
<SfGrid TItem="TItem"
23+
DataSource="DataSource"
24+
AllowSorting="AllowSorting"
25+
AllowPaging="AllowPaging"
26+
@attributes="AdditionalAttributes">
27+
<GridPageSettings PageCount="@PAGE_COUNT"
28+
PageSize="@DEFAULT_PAGE_SIZE"
29+
PageSizes="PageSizes">
30+
</GridPageSettings>
2431
@ChildContent
25-
<GridPageSettings PageCount="PAGE_COUNT" PageSize="DEFAULT_PAGE_SIZE" PageSizes="PageSizes"></GridPageSettings>
2632
</SfGrid>
2733
```
2834

@@ -38,46 +44,54 @@ using System.Threading.Tasks;
3844

3945
namespace SF_Grid_Inheritance.Shared
4046
{
41-
public partial class CustomGrid<TValue> : SfGrid<TValue>
47+
public partial class CustomGrid<TItem> : ComponentBase
4248
{
4349
public const int PAGE_COUNT = 5;
4450
public const int DEFAULT_PAGE_SIZE = 10;
45-
public string[] PageSizes = new string[] { "10", "20", "50" };
46-
IReadOnlyDictionary<string, object> props { get; set; }
47-
public override Task SetParametersAsync(ParameterView parameters)
48-
{
49-
//Assign the additional parameters
50-
props = parameters.ToDictionary();
51-
return base.SetParametersAsync(parameters);
52-
}
53-
protected async override Task OnParametersSetAsync()
54-
{
55-
AllowPaging = true;
56-
AllowSorting = true;
57-
await base.OnParametersSetAsync();
58-
}
51+
52+
[Parameter] public IEnumerable<TItem>? DataSource { get; set; }
53+
[Parameter] public bool AllowPaging { get; set; } = true;
54+
[Parameter] public bool AllowSorting { get; set; } = true;
55+
[Parameter] public string[] PageSizes { get; set; } = new[] { "10", "20", "50" };
56+
[Parameter] public RenderFragment? ChildContent { get; set; }
57+
58+
// Forwards any additional attributes/events to the inner SfGrid
59+
[Parameter(CaptureUnmatchedValues = true)]
60+
public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }
5961
}
6062
}
6163
```
6264

6365
Index.razor
6466

65-
```csharp
67+
```razor
68+
@using SF_Grid_Inheritance.Shared
69+
@using Syncfusion.Blazor.Grids
6670
67-
<CustomGrid DataSource="Orders" TValue="Order"></CustomGrid>
71+
<CustomGrid DataSource="Orders" TItem="Order">
72+
<GridColumns>
73+
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
74+
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer ID" Width="150"></GridColumn>
75+
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
76+
<GridColumn Field=@nameof(Order.OrderDate) HeaderText="Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
77+
</GridColumns>
78+
</CustomGrid>
6879
6980
@code{
70-
public List<Order> Orders { get; set; }
81+
public List<Order> Orders { get; set; } = new();
82+
private static readonly Random _random = new Random();
83+
7184
protected override void OnInitialized()
7285
{
7386
Orders = Enumerable.Range(1, 75).Select(x => new Order()
7487
{
7588
OrderID = 1000 + x,
76-
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
77-
Freight = 2.1 * x,
89+
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[_random.Next(5)],
90+
Freight = Math.Round(2.1 * x, 2),
7891
OrderDate = DateTime.Now.AddDays(-x),
7992
}).ToList();
8093
}
94+
8195
public class Order
8296
{
8397
public int? OrderID { get; set; }
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
---
22
layout: post
33
title: CSS isolation for Blazor DataGrid Component | Syncfusion
4-
description: Checkout and learn here all about applying styles by using CSS isolation in Syncfusion Blazor DataGrid component and more.
4+
description: Check out how to apply styles using CSS isolation in the Syncfusion Blazor DataGrid component with ::deep selectors and scoped wrapper patterns.
55
platform: Blazor
66
control: DataGrid
77
documentation: ug
88
---
99

1010
# CSS isolation for Blazor DataGrid
1111

12-
CSS isolation allows defining component-specific styles by creating a `.razor.css` file that matches the name of the `.razor` file. This ensures that the styles apply only to the intended component without affecting other parts of the application. For example, to apply styles to an `Index` component, create a file named `Index.razor.css` in the same folder as `Index.razor`.
12+
CSS isolation allows defining component-specific styles by creating a **.razor.css** file that matches the name of the **.razor** file. This ensures that the styles apply only to the intended component without affecting other parts of the application. For example, to apply styles to an **Index** component, create a file named **Index.razor.css** in the same folder as **Index.razor**.
1313

14-
To enable CSS isolation for the Grid, it is recommended to wrap the **SfGrid** inside a standard HTML <div> element. This setup helps properly scope the styles when using the **::deep** combinator, which is required to target nested child elements within the isolated styles.
14+
To enable CSS isolation for the Grid, it is recommended to wrap the `SfGrid` inside a standard HTML div element (optionally with a custom class). This setup helps properly scope the styles when using the **::deep** combinator, which is required to target nested child elements rendered by the Grid within isolated styles.
1515

16-
Below is an example of implementing a simple Grid inside the `Index.razor` file:
17-
18-
```csharp
16+
Below is an example of implementing a simple Grid inside the **Index.razor** file:
1917

18+
```razor
2019
Index.razor
2120
2221
@using Syncfusion.Blazor.Grids
2322
24-
<div>
23+
<div class="grid-host">
2524
<SfGrid DataSource="@Orders" AllowPaging="true">
2625
<GridColumns>
2726
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"></GridColumn>
2827
<GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="120"></GridColumn>
29-
<GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" EditType="EditType.DatePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn>
28+
<GridColumn Field=@nameof(Order.OrderDate) HeaderText="Order Date" EditType="EditType.DatePickerEdit" Format="d" TextAlign="TextAlign.Right" Width="130" Type="ColumnType.Date"></GridColumn>
3029
<GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" EditType="EditType.NumericEdit" Width="120"></GridColumn>
3130
<GridColumn Field=@nameof(Order.ShipCountry) HeaderText="Ship Country" EditType="EditType.DropDownEdit" Width="150"></GridColumn>
3231
</GridColumns>
3332
</SfGrid>
3433
</div>
3534
3635
@code{
36+
private static readonly Random random = new Random();
3737
public List<Order> Orders { get; set; }
3838
3939
protected override void OnInitialized()
4040
{
4141
Orders = Enumerable.Range(1, 75).Select(x => new Order()
4242
{
4343
OrderID = 1000 + x,
44-
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
44+
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[random.Next(5)],
4545
Freight = 2.1 * x,
4646
OrderDate = DateTime.Now.AddDays(-x),
47-
ShipCountry = (new string[] { "USA", "UK", "CHINA", "RUSSIA", "INDIA" })[new Random().Next(5)]
47+
ShipCountry = (new string[] { "USA", "UK", "CHINA", "RUSSIA", "INDIA" })[random.Next(5)]
4848
}).ToList();
4949
}
5050
@@ -59,12 +59,13 @@ Index.razor
5959
}
6060
```
6161

62-
```csharp
62+
```css
6363
Index.razor.css
64-
::deep .e-grid .e-altrow {
64+
.grid-host ::deep .e-altrow {
6565
background-color: violet;
6666
}
6767
```
68+
6869
> Please find the sample in this [GitHub location](https://github.com/SyncfusionExamples/How-to-Getting-Started-Blazor-DataGrid-Samples/tree/master/CSS_Isolation).
6970
70-
N> You can get more information on CSS Isolation [here](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-8.0#child-component-support).
71+
N> More information on CSS Isolation is available [here](https://learn.microsoft.com/en-us/aspnet/core/blazor/components/css-isolation?view=aspnetcore-8.0#child-component-support).

blazor/datagrid/how-to/custom-helper-function-inside-loop-with-template.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
22
layout: post
3-
title: Use custom helper inside the loop with templates in Blazor DataGrid | Syncfusion
4-
description: Learn here all about place cancel icon in search bar in Syncfusion Blazor DataGrid.
3+
title: Use Custom Helper with Templates in Blazor DataGrid | Syncfusion
4+
description: Learn how to use a custom helper method inside a column template loop to render a star rating in the Syncfusion Blazor DataGrid.
55
platform: Blazor
66
control: DataGrid
77
documentation: ug
88
---
99

1010
# Use custom helper inside the loop with templates
1111

12-
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid allows you to use custom helpers inside the loop with [Template](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridColumn.html?#Syncfusion_Blazor_Grids_GridColumn_Template) property of a column. This feature enables you to create complex templates that can incorporate additional helper functions.
12+
The Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid supports using custom helpers inside the loop within a column [Template](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridColumn.html#Syncfusion_Blazor_Grids_GridColumn_Template). This enables building flexible templates that incorporate additional logic and helper functions.
1313

14-
The **customer rating** column includes a custom template defined using `Template`. Inside this template, iterates through the item array and generates `<span>` tag, displayed as stars using the CSS below:
14+
The following example renders a customer rating column with a custom template. Inside the template, the code iterates over a fixed range and generates star elements. The **IsRatingGreater** helper method determines which stars to highlight.
1515

1616
```css
1717
.e-grid .rating .star:before {
@@ -24,20 +24,19 @@ The **customer rating** column includes a custom template defined using `Templat
2424
}
2525
```
2626

27-
The class is dynamically assigned based on the result of the `isRatingGreater` method, highlighting the star using the CSS below:
27+
The highlighted state is applied based on the result of the `IsRatingGreater` method:
2828

2929
```css
3030
.e-grid .rating .star.checked {
3131
color: #ffa600;
3232
}
3333
```
3434

35-
This is demonstrated in the following example.
36-
3735
{% tabs %}
3836
{% highlight razor tabtitle="Index.razor" %}
3937

4038
@using Syncfusion.Blazor.Grids
39+
@using System.Linq
4140

4241
<SfGrid DataSource="@Orders" Height="300px">
4342
<GridColumns>
@@ -48,7 +47,7 @@ This is demonstrated in the following example.
4847
@{
4948
var rating = (context as Order)?.Rating ?? 0;
5049
}
51-
<div class="rate">
50+
<div class="rate" aria-label="@($"{rating} out of 5 stars")">
5251
<div class="rating">
5352
@foreach (var i in Enumerable.Range(1, 5))
5453
{

blazor/datagrid/how-to/customize-empty-grid-display-message.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
---
22
layout: post
33
title: Customize Empty Record Template in the Blazor DataGrid | Syncfusion
4-
description: Learn here all about customize the empty record template in Syncfusion Blazor DataGrid.
4+
description: Learn how to customize the EmptyRecordTemplate in the Syncfusion Blazor DataGrid to show a custom message or content when no records are available.
55
platform: Blazor
66
control: DataGrid
77
documentation: ug
88
---
99

1010
# Customize the empty record template in Blazor DataGrid
1111

12-
The empty record template feature in the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid allows you to use custom content such as images, text, or other components, when the Grid doesn't contain any records to display. This feature replaces the default message of 'No records to display' typically shown in the Grid.
13-
14-
To activate this feature, set the [EmptyRecordTemplate](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridTemplates.html#Syncfusion_Blazor_Grids_GridTemplates_EmptyRecordTemplate) feature of the Grid. The `EmptyRecordTemplate` feature expects the HTML element or a function that returns the HTML element.
15-
16-
The following example demonstrates how an image and text can be rendered as a template to indicate that the Grid has no data to display:
12+
The empty record template in the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid enables rendering custom content such as images, text, or other components when the Grid has no records to display. This replaces the default “No records to display” message.
13+
Define the template using the [EmptyRecordTemplate](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridTemplates.html#Syncfusion_Blazor_Grids_GridTemplates_EmptyRecordTemplate) within `<GridTemplates>`. In Blazor, this is a Razor fragment (RenderFragment), allowing any valid Razor markup or components. The empty record template is shown whenever the data source results in zero rows, including initial load, after filtering or searching yields no matches, or after deletions remove all rows.
1714

1815
{% tabs %}
1916
{% highlight razor tabtitle="Index.razor" %}

blazor/datagrid/how-to/define-events-programmatically.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
11
---
22
layout: post
33
title: Define events in Blazor DataGrid Component | Syncfusion
4-
description: Checkout and learn here all about defining events programmatically in the Syncfusion Blazor DataGrid component and more.
4+
description: Learn how to define GridEvents programmatically in the Syncfusion Blazor DataGrid using a component reference and EventCallbackFactory.
55
platform: Blazor
66
control: DataGrid
77
documentation: ug
88
---
99

10-
# Define the GridEvents programmatically in Blazor DataGrid
10+
# Define Grid events programmatically in Blazor DataGrid
1111

12-
In the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid, events are typically defined using the GridEvents child Razor component. As an alternative, Grid events can also be configured programmatically by accessing the Grid instance through a component reference. This method is useful when events need to be assigned dynamically during the application lifecycle.
12+
In the Syncfusion<sup style="font-size:70%">&reg;</sup> Blazor DataGrid, events are typically defined using the [GridEvents](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridEvents-1.html#) child Razor component. As an alternative, Grid events can also be configured programmatically by accessing the Grid instance through a component reference. This approach is useful when events need to be assigned dynamically during the application lifecycle.
1313

14-
To define events programmatically:
14+
> Note: Assigning **Grid.GridEvents** programmatically replaces any events declared via the `<GridEvents>` markup for that Grid instance.
1515
16-
* Set a reference to the Grid using the `@ref` directive.
17-
* After the Grid is rendered, assign the `GridEvents` property within the `OnAfterRenderAsync` lifecycle method.
18-
* Use the `EventCallbackFactory` to create event handlers, ensuring they are correctly bound to the component context.
16+
To define events programmatically:
1917

20-
The following example demonstrates how to configure the [DataBound](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridEvents-1.html#Syncfusion_Blazor_Grids_GridEvents_1_DataBound) event programmatically to perform custom logic after the Grid's data has been populated:
18+
- Set a reference to the Grid using the **@ref** directive.
19+
- After the Grid is rendered, assign the **GridEvents** property within the **OnAfterRenderAsync** lifecycle method (guarded by **firstRender**).
20+
- Use the **EventCallbackFactory** to create event handlers bound to the component context.
2121

2222
{% tabs %}
2323
{% highlight razor tabtitle="Index.razor" %}
2424
@using Syncfusion.Blazor.Grids
25+
@inject IJSRuntime JS
2526

2627
<SfGrid @ref="Grid" DataSource="@Orders">
27-
</SfGrid>
28+
</SfGrid>
29+
2830
@code{
29-
SfGrid<Order> Grid { get; set; }
31+
SfGrid<Order> Grid { get; set; }
3032
public List<Order> Orders { get; set; }
31-
protected override Task OnAfterRenderAsync(bool firstRender)
32-
{
33-
if (Grid != null)
34-
{
33+
34+
protected override Task OnAfterRenderAsync(bool firstRender)
35+
{
36+
if (firstRender && Grid != null)
37+
{
3538
Grid.GridEvents = new GridEvents<Order>()
3639
{
3740
DataBound = new EventCallbackFactory().Create<object>(this, DataBoundHandler)
3841
};
39-
}
40-
return base.OnAfterRenderAsync(firstRender);
41-
}
42+
}
43+
return base.OnAfterRenderAsync(firstRender);
44+
}
45+
4246
public async Task DataBoundHandler()
4347
{
44-
await JS.InvokeVoidAsync("alert","Grid data loaded successfully.");
48+
await JS.InvokeVoidAsync("alert", "Grid data loaded successfully.");
4549
}
50+
4651
protected override void OnInitialized()
4752
{
4853
Orders = new List<Order>()
@@ -58,15 +63,15 @@ The following example demonstrates how to configure the [DataBound](https://help
5863
new Order() { OrderID = 1009, EmployeeID = 9, OrderDate = DateTime.Now.AddDays(-9), ShipCountry = "Sweden" }
5964
};
6065
}
66+
6167
public class Order
6268
{
6369
public int? OrderID { get; set; }
6470
public int? EmployeeID { get; set; }
6571
public DateTime? OrderDate { get; set; }
66-
public string ShipCountry { get; set; }
72+
public string ShipCountry { get; set; }
6773
}
6874
}
69-
7075
{% endhighlight %}
7176
{% endtabs %}
7277

0 commit comments

Comments
 (0)