Back to Devexpress

GridExportOptions.CustomizeCell Property

blazor-devexpress-dot-blazor-dot-gridexportoptions.md

latest7.0 KB
Original Source

GridExportOptions.CustomizeCell Property

Allows you to customize a cell in the exported file.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
public Action<GridExportCustomizeCellEventArgs> CustomizeCell { get; set; }

Property Value

TypeDescription
Action<GridExportCustomizeCellEventArgs>

A delegate method that customize the cell.

|

Remarks

The CustomizeCell action allows you to perform the following actions.

  • Substitute the cell value (Value).
  • Insert a hyperlink in the cell (Hyperlink).
  • Customize the cell’s format (Formatting).

Set the Handled property to true to apply the specified settings. Otherwise, the cell is exported with the default settings.

The table below lists properties that you can use to get additional information about the processed cell depending on the processed area type (AreaType).

Area TypeUseful propertyProperty that returns null
HeaderColumnFieldNameDataItem, SummaryItem, GroupFieldName
GroupHeaderColumnFieldName, GroupFieldNameDataItem, SummaryItem
DataAreaColumnFieldName, DataItemSummaryItem, GroupFieldName
GroupFooterColumnFieldName, GroupFieldName, SummaryItemDataItem
TotalFooterColumnFieldName, SummaryItemDataItem, GroupFieldName

The GetRowValue(String) method returns the value of the specified data field in the current row.

Run Demo: Grid - Export DataView Example: Customize Export Settings

razor
@using System.Xml;
<DxGrid @ref="Grid" Data="@Data" >
    <Columns>
        <DxGridDataColumn FieldName="OrderDate" />
        <DxGridDataColumn FieldName="CompanyName" />
        <DxGridDataColumn FieldName="Country" />
        <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" />
        <DxGridDataColumn FieldName="Quantity" />
        <DxGridDataColumn FieldName="Total" UnboundType="GridUnboundColumnType.Decimal" 
                          DisplayFormat="c" UnboundExpression="[UnitPrice]*[Quantity]" />
    </Columns>
</DxGrid>
<DxButton Text="Export in Euro" Click="ExportXlsx_Click" />

@code {
    object Data { get; set; }
    IGrid Grid { get; set; }
    protected override async Task OnInitializedAsync() {
        var invoices = await NwindDataService.GetInvoicesAsync();
        var customers = await NwindDataService.GetCustomersAsync();
        Data = invoices.OrderBy(i => i.OrderDate).Join(customers, i => i.CustomerId, c => c.CustomerId, (i, c) => {
            return new {
                OrderDate = i.OrderDate,
                CompanyName = c.CompanyName,
                City = i.City,
                Region = i.Region,
                Country = i.Country,
                UnitPrice = i.UnitPrice,
                Quantity = i.Quantity
            };
        });
    }
    decimal GetRate() {
        string daylyRate = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml";
        decimal rate = 0;
        XmlTextReader reader = new XmlTextReader(daylyRate);
        while (reader.Read()) {
            if (reader.NodeType == XmlNodeType.Element && reader.MoveToAttribute("currency")) {
                if (reader.Value == "USD") {
                    reader.MoveToAttribute("rate");
                    rate = Convert.ToDecimal(reader.Value);
                    break;
                }

            }
        }
        return rate;
    }

    async Task ExportXlsx_Click() {
        decimal currentRate = GetRate();
        var options = new GridXlExportOptions();
        options.DocumentCulture = new CultureInfo("fr-FR");
        options.SheetName = "(Rate = " + currentRate + ")";
        options.CustomizeCell = e => {
            if (e.AreaType == DevExpress.Export.SheetAreaType.DataArea && (e.ColumnFieldName == "Total" || e.ColumnFieldName == "UnitPrice")) {
                e.Value = (decimal)e.Value / currentRate;
                // Highlight data cells where Total > 1000€
                if (e.ColumnFieldName == "Total" && (decimal)e.Value > 1000) {
                    e.Formatting.BackColor = System.Drawing.Color.LightGreen;
                }
                e.Handled = true;
            }
        };
        await Grid.ExportToXlsxAsync("ExportResult", options);
    }
}

For additional information about data export in the Grid component, refer to the following topic: Export Blazor Grid Data to XLS/XLSX.

See Also

GridExportOptions Class

GridExportOptions Members

DevExpress.Blazor Namespace