Back to Devexpress

DxGrid.StartEditDataItemAsync(Object, String) Method

blazor-devexpress-dot-blazor-dot-dxgrid-dot-starteditdataitemasync-x28-system-dot-object-system-dot-string-x29.md

latest8.6 KB
Original Source

DxGrid.StartEditDataItemAsync(Object, String) Method

Starts editing the specified data item.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
public Task StartEditDataItemAsync(
    object dataItem,
    string fieldName = null
)

Parameters

NameTypeDescription
dataItemObject

A data item.

|

Optional Parameters

NameTypeDefaultDescription
fieldNameStringnull

A data source field name that defines the column cell in which an editor in EditCell mode is to be displayed. If this parameter is omitted, the method displays an editor in the first data column whose DataRowEditorVisible equals true.

|

Returns

TypeDescription
Task

A task that is completed when the row is in edit mode.

|

Remarks

The Grid’s EditMode property specifies how users edit Grid data. The StartEditDataItemAsync method behaves as follows depending on the active edit mode:

EditForm (Default)Displays the edit form for the specified data item and focuses the first editor in this form.PopupEditFormInvokes the pop-up edit form for the specified data item and focuses the first editor in this form.EditRowDisplays the edit row for the specified data item and focuses the first editor in this row.EditCell

Displays and focuses an in-place editor in the cell associated with the specified field of the target data item. The method ignores the fieldName parameter in the following cases:

  • No column is bound to the specified field.
  • The DataRowEditorVisible property of the column bound to the specified field is disabled.

When the fieldName parameter is omitted or ignored, the method displays an editor in the first data column whose DataRowEditorVisible equals true.

Declare a DxGridCommandColumn object in the Columns template to display predefined New , Edit , and Delete command buttons in the Grid component. Alternatively, you can create custom command elements inside or outside the Grid. Handle a custom element’s click event and call the StartEditDataItemAsync method to start editing the specified data item.

Before you start editing a record, call the MakeDataItemVisibleAsync(Object) method to display this record.

The following code snippet displays Open Iconic icons in command buttons:

razor
@page "/"
@using CommandButtonsWithIcons.Data
@inject WeatherForecastService ForecastService

<div>
    <DxGrid Data="forecasts" 
            @ref="MyGrid" 
            KeyFieldName="ID" 
            DataItemDeleting="OnDataItemDeleting"
            EditModelSaving="OnEditModelSaving">
        <Columns>
            <DxGridCommandColumn>
                <HeaderTemplate>
                    <a class="oi oi-plus link-decoration" @onclick="@(() => MyGrid.StartEditNewRowAsync())" href="javascript:void(0);"></a>
                </HeaderTemplate>
                <CellDisplayTemplate>
                    <a class="oi oi-pencil link-decoration" @onclick="@(() => MyGrid.StartEditRowAsync(context.VisibleIndex))" href="javascript:void(0);"></a>
                    <a class="oi oi-x link-decoration" @onclick="@(() => MyGrid.ShowDataItemDeleteConfirmation(context.DataItem))" href="javascript:void(0);"></a>
                </CellDisplayTemplate>
            </DxGridCommandColumn>
            <DxGridDataColumn FieldName=@nameof(WeatherForecast.TemperatureC) Caption="Temp. (C)" />
            <DxGridDataColumn FieldName=@nameof(WeatherForecast.TemperatureF) Caption="Temp. (F)" />
            <DxGridDataColumn FieldName=@nameof(WeatherForecast.Summary) Caption="Summary" />
            <DxGridDataColumn FieldName=@nameof(WeatherForecast.Date) DisplayFormat="dd/MM/yyyy" />
        </Columns>
        <EditFormTemplate Context="EditFormContext">
            <DxFormLayout>
                <DxFormLayoutItem Caption="Temp. (C)">
                    @EditFormContext.GetEditor("TemperatureC")
                </DxFormLayoutItem>
                <DxFormLayoutItem Caption="Summary">
                    @EditFormContext.GetEditor("Summary")
                </DxFormLayoutItem>
                <DxFormLayoutItem Caption="Date">
                    @EditFormContext.GetEditor("Date")
                </DxFormLayoutItem>
            </DxFormLayout>
        </EditFormTemplate>
    </DxGrid>
</div>

@code {
    string[]? summaries;
    private List<WeatherForecast>? forecasts;
    DxGrid? MyGrid;
    protected override async Task OnInitializedAsync() {
        forecasts = await ForecastService.GetForecastAsync();
        summaries = ForecastService.Summaries;
    }
    async Task OnEditModelSaving(GridEditModelSavingEventArgs e) {
        forecasts = await ForecastService.ChangeForecastAsync((WeatherForecast)e.EditModel);
    }
    async Task OnDataItemDeleting(GridDataItemDeletingEventArgs e) {
        if (e.DataItem != null)
            await ForecastService.Remove((WeatherForecast)e.DataItem);
    }
}
csharp
namespace CommandButtonsWithIcons.Data
{
    public class WeatherForecast
    {
        public int ID { get; set; }
        public DateTime Date { get; set; }
        public int TemperatureC { get; set; }
        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
        public string? Summary { get; set; }
    }
}
csharp
namespace CommandButtonsWithIcons.Data {
    public class WeatherForecastService {
        public readonly string[] Summaries = new[] {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };
        private List<WeatherForecast>? Forecasts;
        public Task<List<WeatherForecast>> GetForecastAsync() {
            if (Forecasts == null) {
                var rnd = new Random();
                Forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast {
                    ID = index,
                    Date = DateTime.Today.AddDays(index),
                    TemperatureC = rnd.Next(-20, 55),
                    Summary = Summaries[rnd.Next(Summaries.Length)]
                }).ToList();
            }
            return Task.FromResult(Forecasts);
        }
        public Task<List<WeatherForecast>> ChangeForecastAsync(WeatherForecast changed) {
            var orginalForecast = Forecasts.FirstOrDefault(i => i.ID == changed.ID);
            if (orginalForecast != null) {
                orginalForecast.TemperatureC = changed.TemperatureC;
                orginalForecast.Summary = changed.Summary;
                orginalForecast.Date = changed.Date;
            }
            else {
                Forecasts.Add(changed);
            }
            return Task.FromResult(Forecasts);
        }
        public Task<List<WeatherForecast>> Remove(WeatherForecast forecast) {
            Forecasts.Remove(forecast);
            return Task.FromResult(Forecasts);
        }
    }
}
css
.link-decoration {
    text-decoration: none;
}

View Example: Use icons instead of default command buttons

For information on how to enable data editing, refer to the following topic: Edit Data in Blazor Grid.

Implements

StartEditDataItemAsync(Object, String)

See Also

DxGrid Class

DxGrid Members

DevExpress.Blazor Namespace