You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor Syncfusion DataGrid增删改命令异常求助

Blazor Syncfusion DataGrid 功能异常修复方案

针对你遇到的Delete按钮不刷新、Edit无法获取修改后数据且无法退出编辑模式的问题,以下是具体修复步骤和修改后的代码:

核心问题分析

  1. 缺少主键配置:Syncfusion Grid需要主键字段来唯一识别行数据,否则编辑、删除操作无法正确关联记录
  2. Edit数据获取错误:使用了原始数据args.RowData而非修改后的数据args.Data
  3. UI刷新不及时:删除/保存后未强制触发组件状态刷新
  4. 编辑模式混淆:代码中设置的是弹窗编辑(Normal),但你需要的是行内编辑(Inline)

修复步骤

1. 配置Grid主键

在<SfGrid>标签中添加PrimaryKey属性,指定实体的主键字段(假设Tarefa实体有Id字段)

2. 修正Edit数据获取逻辑

在OnActionBegin的Edit分支中,替换args.RowData为args.Data获取修改后的数据

3. 优化Delete操作逻辑

直接通过args.RowData获取待删除记录,避免依赖选中记录集合的潜在问题

4. 强制UI刷新

在OnActionComplete更新数据源后,调用StateHasChanged()确保组件重新渲染

5. 调整编辑模式(可选)

将GridEditSettings的Mode改为EditMode.Inline,匹配你需要的行内编辑需求


修改后的完整代码

@page "/datagrid-features"
@using BlazorUI.Models
@rendermode InteractiveServer
@inject ITarefaService taskService
@using Syncfusion.Blazor.Grids

<PageTitle>DataGrid</PageTitle>
<h2>DataGrid</h2>
<br/>
<div id="ControlRegion">
    <SfGrid ID="Grid" DataSource="@Tasks" @ref="Grid"
            AllowPaging="true" AllowFiltering="true" AllowReordering="true" AllowResizing="true"
            AllowGrouping="true" AllowExcelExport="true" AllowSelection="true" AllowSorting="true"
            Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update", "Search"})"
            PrimaryKey="@nameof(Tarefa.Id)"
            Height="315" Width="900">

        <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Inline"></GridEditSettings>

        <GridFilterSettings Type="FilterType.FilterBar"></GridFilterSettings>

        <GridPageSettings PageSizes="true"></GridPageSettings>

        <GridSelectionSettings Type="SelectionType.Multiple"></GridSelectionSettings>

        <GridEvents OnToolbarClick="ToolbarClick" OnActionBegin="OnActionBegin" OnActionComplete="OnActionComplete" TValue="Tarefa"></GridEvents>

        <GridColumns>
            <GridColumn Type="ColumnType.CheckBox" Width="50"></GridColumn>

            <!-- Task Name -->
            <GridColumn Field=@nameof(Tarefa.TaskName) HeaderText="Task Name"
                        ValidationRules="@(new ValidationRules{ Required=true })" Width="200"></GridColumn>

            <!-- Start Date -->
            <GridColumn Field=@nameof(Tarefa.StartDate) HeaderText="Start Date"
                        EditType="EditType.DatePickerEdit" Format="yyyy-MM-dd"
                        TextAlign="TextAlign.Right" Width="130"
                        Type="ColumnType.Date"></GridColumn>

            <!-- End Date -->
            <GridColumn Field=@nameof(Tarefa.EndDate) HeaderText="End Date"
                        EditType="EditType.DatePickerEdit" Format="yyyy-MM-dd"
                        TextAlign="TextAlign.Right" Width="130"
                        Type="ColumnType.Date"></GridColumn>

            <!-- Is Done -->
            <GridColumn Field=@nameof(Tarefa.IsDone) HeaderText="Is Task Done"
                        EditType="EditType.BooleanEdit" Width="120" Type="ColumnType.Boolean"></GridColumn>
        </GridColumns>
    </SfGrid>
</div>

<style>
    .ulstyle {
        margin: 0px;
        padding-left: 20px;
        display: inline-block;
    }
    .list {
        float: left;
        line-height: 20px;
        margin: 10px;
        min-width: 200px;
    }
</style>

@code {
    public List<Tarefa>? Tasks { get; set; }
    SfGrid<Tarefa>? Grid;

    protected override async Task OnInitializedAsync()
    {
        Tasks = await taskService.GetTarefasAsync();
    }

    public async Task ToolbarClick(Syncfusion.Blazor.Navigations.ClickEventArgs args)
    {
        if (args.Item.Id == "Grid_csvexport")
        {
            await this.Grid?.ExportToCsvAsync();
        }
    }

    public async Task OnActionBegin(ActionEventArgs<Tarefa> args)
    {
        if (args.RequestType == Syncfusion.Blazor.Grids.Action.Save)
        {
            if (args.Action == "Add")
            {
                var newTask = args.RowData as Tarefa;
                if (newTask != null)
                    await taskService.AdicionarTarefaAsync(newTask);
            }
            else if (args.Action == "Edit")
            {
                var updatedTask = args.Data as Tarefa;
                if (updatedTask != null)
                    await taskService.AtualizarTarefaAsync(updatedTask);
            }
        }
        else if (args.RequestType == Syncfusion.Blazor.Grids.Action.Delete)
        {
            var deletedTask = args.RowData as Tarefa;
            if (deletedTask != null)
                await taskService.RemoverTarefaAsync(deletedTask);
        }
    }

    private async Task OnActionComplete(ActionEventArgs<Tarefa> args)
    {
        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save) || args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Delete))
        {
            Tasks = await taskService.GetTarefasAsync();
            StateHasChanged();
        }
    }
}

内容的提问来源于stack exchange,提问作者AntipodalMath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 15:44:53