Skip to content

Commit d804941

Browse files
committed
added new post Blazor Radzen .NET 8 DataGrid InCellEdit
1 parent f354fea commit d804941

File tree

3 files changed

+237
-0
lines changed

3 files changed

+237
-0
lines changed
66.1 KB
Loading
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
---
2+
title: "Blazor Radzen .NET 8 DataGrid InCellEdit"
3+
date: 2025-07-20T00:00:00+00:00
4+
hero: blazor_radzen_dotnet8.jpg
5+
description: Blazor Radzen .NET 8 DataGrid InCellEdit
6+
menu:
7+
dotnet:
8+
name: Blazor Radzen .NET 8 DataGrid InCellEdit
9+
identifier: blazor-radzen-dotnet8-datagrid-incelledit
10+
weight: -20250720
11+
tags: [dotnet8, .NET8, Blazor, Radzen, DataGrid, InCellEdit]
12+
categories: [dotnet8, .NET8, Blazor, Radzen, DataGrid, InCellEdit]
13+
14+
author:
15+
name: Akif T.
16+
---
17+
18+
<p class="d-flex justify-content-center">
19+
<img src="blazor_radzen_dotnet8.jpg" alt="blazor_radzen_dotnet8" title="blazor_radzen_dotnet8" style="border-radius: 20px;"><br>
20+
</p>
21+
22+
23+
#### **Blazor Radzen .NET 8 DataGrid InCellEdit**
24+
25+
<p class="d-flex justify-content-center">
26+
<img src="preview1.jpg" alt="preview1" title="preview1" style="border-radius: 20px;"><br>
27+
<p>
28+
29+
Blazor: ```Blazor``` A framework for building interactive web applications using C# instead of JavaScript. It allows developers to create rich web UIs with a component-based architecture.
30+
Radzen: ```Radzen``` A set of UI components for Blazor that simplifies the development of web applications. It provides a variety of ```controls```, including DataGrids, which are essential for displaying and managing data.
31+
.NET 8: ```.NET 8``` is the latest version of the ```.NET framework```, which includes enhancements and new features that improve performance, security, and developer productivity.
32+
DataGrid: ```DataGrid``` A component that displays data in a tabular format, allowing for operations like sorting, filtering, and editing.
33+
InCellEdit: ```InCellEdit``` A feature that enables users to edit data directly within the cells of the DataGrid.
34+
35+
##### **Index.razor**
36+
<kbd>Index.razor</kbd>
37+
```
38+
@page "/BlogPost"
39+
40+
<PageTitle>Posts</PageTitle>
41+
42+
<style>
43+
.table-cell-edited {
44+
position: relative;
45+
}
46+
47+
.table-cell-edited::after {
48+
content: "";
49+
position: absolute;
50+
top: 0;
51+
right: 0;
52+
width: 0;
53+
height: 0;
54+
border-top: 10px solid red;
55+
border-left: 10px solid transparent;
56+
}
57+
</style>
58+
59+
<RadzenRow>
60+
<RadzenColumn SizeSM="12" SizeMD="12" SizeLG="4">
61+
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center">
62+
<RadzenText Text="Posts" TextStyle="TextStyle.H5" />
63+
@* <RadzenButton Text="Create" Icon="add_circle_outline"
64+
Click="NavigatetoCreate"
65+
ButtonStyle="ButtonStyle.Success" class="rz-mb-2 rz-p-2" /> *@
66+
</RadzenStack>
67+
</RadzenColumn>
68+
</RadzenRow>
69+
70+
71+
<RadzenDataGrid @ref="blogPostsGrid" KeyProperty="Id" IsLoading="@isLoading" Count="@totalCount" ShowPagingSummary=true AllowAlternatingRows="false" AllowFiltering="true" FilterMode="FilterMode.Advanced" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" AllowPaging="true" PagerHorizontalAlign="HorizontalAlign.Center" PageSize="@itemPageSize" AllowSorting="true" Data="@blogPosts" TItem="BlogPostViewModel" LoadData="@LoadData" RowUpdate="@OnUpdateRow" Sort="@(args => Reset())" Page="@(args => Reset())" Filter="@(args => Reset())" CellClick="@OnCellClick">
72+
<Columns>
73+
<RadzenDataGridColumn TItem="BlogPostViewModel" Property="Id" Filterable="false" Title="Id" Frozen="true" Width="50px" MinWidth="50px" TextAlign="TextAlign.Center" />
74+
<RadzenDataGridColumn TItem="BlogPostViewModel" Property="Title" Title="Title" IsInEditMode="@IsEditing" CalculatedCssClass="@IsEdited">
75+
<EditTemplate Context="blogPost">
76+
<RadzenTextBox @bind-Value="blogPost.Title" Name="Title" style="width: 100%" aria-label="Enter Title" />
77+
<RadzenRequiredValidator Text="Title is required" Component="Title" Popup="true" />
78+
</EditTemplate>
79+
</RadzenDataGridColumn>
80+
<RadzenDataGridColumn TItem="BlogPostViewModel" Property="Content" Title="Content" IsInEditMode="@IsEditing" CalculatedCssClass="@IsEdited">
81+
<EditTemplate Context="blogPost">
82+
<RadzenTextBox @bind-Value="blogPost.Content" Name="Content" style="width: 100%" aria-label="Enter Content" />
83+
<RadzenRequiredValidator Text="Content is required" Component="Content" Popup="true" />
84+
</EditTemplate>
85+
</RadzenDataGridColumn>
86+
87+
</Columns>
88+
</RadzenDataGrid>
89+
90+
@code {
91+
92+
const int itemPageSize = 10;
93+
private bool isLoading;
94+
private int totalCount;
95+
RadzenDataGrid<BlogPostViewModel>? blogPostsGrid;
96+
private IEnumerable<BlogPostViewModel>? blogPosts;
97+
98+
List<BlogPostViewModel> blogPostsToUpdate = new List<BlogPostViewModel>();
99+
100+
string columnEditing;
101+
List<KeyValuePair<int, string>> editedFields = new List<KeyValuePair<int, string>>();
102+
IRadzenFormComponent editor;
103+
bool editorFocused;
104+
105+
106+
protected override async Task OnInitializedAsync()
107+
{
108+
await base.OnInitializedAsync();
109+
}
110+
111+
protected override async Task OnAfterRenderAsync(bool firstRender)
112+
{
113+
await base.OnAfterRenderAsync(firstRender);
114+
115+
if (!editorFocused && editor != null)
116+
{
117+
editorFocused = true;
118+
119+
try
120+
{
121+
await editor.FocusAsync();
122+
}
123+
catch { }
124+
}
125+
}
126+
127+
private async Task LoadData(LoadDataArgs args)
128+
{
129+
isLoading = true;
130+
131+
var result = await BlogPostService.GetBlogPostsAsync(filter: args.Filter, top: args.Top, skip: args.Skip, orderby: args.OrderBy, count: true);
132+
133+
blogPosts = Mapper.Map<IEnumerable<BlogPost>, IEnumerable<BlogPostViewModel>>(result.Result);
134+
totalCount = result.TotalCount;
135+
136+
isLoading = false;
137+
}
138+
139+
private bool IsEditing(string columnName, BlogPostViewModel blogPostViewModel)
140+
{
141+
return columnEditing == columnName && blogPostsToUpdate.Contains(blogPostViewModel);
142+
}
143+
144+
private string IsEdited(RadzenDataGridColumn<BlogPostViewModel> column, BlogPostViewModel blogPostViewModel)
145+
{
146+
return editedFields.Where(c => c.Key == blogPostViewModel.Id && c.Value == column.Property).Any() ?
147+
"table-cell-edited" :
148+
string.Empty;
149+
}
150+
151+
private async Task OnCellClick(DataGridCellMouseEventArgs<BlogPostViewModel> args)
152+
{
153+
if (!blogPostsGrid.IsValid ||
154+
(blogPostsToUpdate.Contains(args.Data) && columnEditing == args.Column.Property)) return;
155+
156+
if (blogPostsToUpdate.Any())
157+
{
158+
editedFields.Add(new(blogPostsToUpdate.First().Id, columnEditing));
159+
await Update();
160+
}
161+
162+
columnEditing = args.Column.Property;
163+
164+
await EditRow(args.Data);
165+
}
166+
167+
private void Reset(BlogPostViewModel blogPostViewModel = null)
168+
{
169+
editorFocused = false;
170+
171+
if (blogPostViewModel != null)
172+
{
173+
blogPostsToUpdate.Remove(blogPostViewModel);
174+
}
175+
else
176+
{
177+
blogPostsToUpdate.Clear();
178+
}
179+
}
180+
181+
private async Task Update()
182+
{
183+
editorFocused = false;
184+
185+
if (blogPostsToUpdate.Any())
186+
{
187+
await blogPostsGrid.UpdateRow(blogPostsToUpdate.First());
188+
}
189+
}
190+
191+
private async Task EditRow(BlogPostViewModel blogPost)
192+
{
193+
Reset();
194+
blogPostsToUpdate.Add(blogPost);
195+
await blogPostsGrid.EditRow(blogPost);
196+
}
197+
198+
async Task OnUpdateRow(BlogPostViewModel blogPostViewModel)
199+
{
200+
Reset(blogPostViewModel);
201+
202+
var blogPost = Mapper.Map<BlogPostViewModel, BlogPost>(blogPostViewModel);
203+
bool result = await BlogPostService.UpdateBlogPostAsync(blogPostViewModel.Id, blogPost);
204+
}
205+
206+
}
207+
208+
```
209+
210+
Radzen's DataGrid to implement in-cell editing for blog posts. This feature allows users to edit data directly within the grid, enhancing user experience and efficiency. We will break down the code provided, explaining its structure and functionality in detail.
211+
212+
213+
```Cell Click Event```: Handles the logic when a cell is clicked, allowing for editing.
214+
```
215+
private async Task OnCellClick(DataGridCellMouseEventArgs<BlogPostViewModel> args)
216+
{
217+
if (!blogPostsGrid.IsValid || (blogPostsToUpdate.Contains(args.Data) && columnEditing == args.Column.Property)) return;
218+
219+
if (blogPostsToUpdate.Any())
220+
{
221+
editedFields.Add(new(blogPostsToUpdate.First().Id, columnEditing));
222+
await Update();
223+
}
224+
225+
columnEditing = args.Column.Property;
226+
await EditRow(args.Data);
227+
}
228+
```
229+
230+
By allowing users to ```edit``` blog post titles and content directly within the ```grid```, the application enhances ```usability and interactivity```. The combination of ```asynchronous data loading```, ```event handling```, and ```conditional rendering``` creates a robust and responsive user interface. This approach not only streamlines the editing process but also ensures that users have a clear understanding of which ```fields``` have been modified, thanks to the visual cues provided by the ```CSS styles```.
231+
232+
233+
234+
235+
#### **Source**
236+
Full source code is available at this repository in GitHub:
237+
https://github.com/akifmt/DotNetCoding/tree/main/src/BlazorAppRadzenNet8DataGridInCellEdit
173 KB
Loading

0 commit comments

Comments
 (0)