Back to Devexpress

GridExportOptions.ExportSelectedRowsOnly Property

blazor-devexpress-dot-blazor-dot-gridexportoptions-495a533a.md

latest5.0 KB
Original Source

GridExportOptions.ExportSelectedRowsOnly Property

Specifies whether the Grid exports only selected rows.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
[DefaultValue(false)]
public override bool ExportSelectedRowsOnly { get; set; }

Property Value

TypeDefaultDescription
Booleanfalse

true to export the selected rows only; false to export all Grid rows.

|

Remarks

Set the ExportSelectedRowsOnly property to true to export selected rows only. To implement this behavior when the component is bound to a GridDevExtremeDataSource<T>, you must also specify the KeyFieldName property.

When ExportSelectedRowsOnly is enabled, the Grid component ignores group settings and exports records as flat data. To preserve row hierarchy, set SelectedRowsExportMode to KeepGrouping.

The following example exports selected records and corresponding group rows to XLSX:

razor
@rendermode InteractiveServer
@inject WeatherForecastService ForecastService

<DxGrid @ref="Grid" Data="@forecasts" ShowGroupPanel="true">
    <Columns>
        <DxGridSelectionColumn Width="60px" AllowSelectAll="true" />
        <DxGridDataColumn Caption="Date" FieldName="Date" />
        <DxGridDataColumn Caption="Temperature (C)" FieldName="TemperatureC" />
        <DxGridDataColumn Caption="Temperature (F)" FieldName="TemperatureF" />
        <DxGridDataColumn Caption="Summary" FieldName="Summary" GroupIndex="0" />
    </Columns>
    <ToolbarTemplate>
        <DxToolbar>
            <DxToolbarItem Text="Export to XLSX" Click="ExportXlsx_Click" BeginGroup="true" />
        </DxToolbar>
    </ToolbarTemplate>
</DxGrid>

@code {
    IGrid Grid;
    object forecasts;

    protected override async Task OnInitializedAsync() {
        forecasts = await ForecastService.GetForecastAsync();
    }
    async Task ExportXlsx_Click() {
        await Grid.ExportToXlsxAsync("ExportResult", new GridXlExportOptions() {
            ExportSelectedRowsOnly = true,
            SelectedRowsExportMode = GridSelectedRowsExportMode.KeepGrouping
        });
    }
}
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, 25).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);
    }
}
csharp
// ...
builder.Services.AddSingleton<WeatherForecastService>();

Read Tutorial: Export to XLS/XLSX Run Demo: Grid - Export Data

See Also

GridExportOptions Class

GridExportOptions Members

DevExpress.Blazor Namespace