Back to Devexpress

GridDataRowCommandContext.RowVisibleIndex Property

blazor-devexpress-dot-blazor-dot-griddatarowcommandcontext-e107b9b8.md

latest5.1 KB
Original Source

GridDataRowCommandContext.RowVisibleIndex Property

Returns the visible index of the target row.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
public int RowVisibleIndex { get; }

Property Value

TypeDescription
Int32

A row visible index.

|

Remarks

The DevExpress Blazor Grid allows you to display context menus with predefined and custom commands. Use the ContextMenus property to activate context menus for specific Grid elements. Handle the CustomizeContextMenu event to modify the menu item collection. Use the Context event argument to identify the target Grid element and obtain contextual information.

When the target element is a data row cell, the Context property returns a GridDataRowCommandContext object. Use the object’s RowVisibleIndex property to obtain the target row’s visible index.

Example

The following code snippet adds a custom Delete command to the row context menu:

razor
@inject WeatherForecastService ForecastService

<DxGrid Data="@forecasts"
        DataItemDeleting="OnDataItemDeleting"
        ContextMenus="GridContextMenus.DataRow"
        CustomizeContextMenu="OnCustomizeContextMenu">
    <Columns>
        <DxGridSelectionColumn />
        <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>
</DxGrid>

@code {
    private List<WeatherForecast>? forecasts;

    protected override async Task OnInitializedAsync() {
        forecasts = await ForecastService.GetForecastAsync();
        summaries = ForecastService.Summaries;
    }
    async Task OnDataItemDeleting(GridDataItemDeletingEventArgs e) {
        if (e.DataItem != null)
            await ForecastService.Remove((WeatherForecast)e.DataItem);
    }
    void OnCustomizeContextMenu(GridCustomizeContextMenuEventArgs args) {
        if (args.Context is GridDataRowCommandContext rowContext) {
            args.Items.AddCustomItem("Delete", () => {
                args.Grid.ShowRowDeleteConfirmation(rowContext.RowVisibleIndex);
            });
        }
    }
}
csharp
using System;

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

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 originalForecast = Forecasts.FirstOrDefault(i => i.ID == changed.ID);
        if (originalForecast != null) {
            originalForecast.TemperatureC = changed.TemperatureC;
            originalForecast.Summary = changed.Summary;
            originalForecast.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);
    }
}
csharp
// ...
builder.Services.AddSingleton<WeatherForecastService>();

See Also

GridDataRowCommandContext Class

GridDataRowCommandContext Members

DevExpress.Blazor Namespace