Back to Devexpress

GridDocumentExportOptions.CustomizeColumn Property

blazor-devexpress-dot-blazor-dot-griddocumentexportoptions-942ad4d7.md

latest6.0 KB
Original Source

GridDocumentExportOptions.CustomizeColumn Property

Allows you to customize table columns in the exported document.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
public Action<GridDocumentExportCustomizeColumnEventArgs> CustomizeColumn { get; set; }

Property Value

TypeDescription
Action<GridDocumentExportCustomizeColumnEventArgs>

A delegate method that customizes table columns.

|

Property Paths

You can access this nested property as listed below:

Object TypePath to CustomizeColumn
GridExportEventArgs

.DocumentOptions .CustomizeColumn

|

Remarks

When exporting Grid data to PDF, handle the CustomizeColumn event to perform the following actions:

Identify ColumnsUse FieldName and ColumnIndex event arguments to determine the processed column and its position in the output table.Display/Hide ColumnsUse the IsHidden event argument to display/hide the processed column.Resize ColumnsSpecify the Width event argument to modify the column width.Style ColumnsSpecify the DataCellStyle event argument to style all data cells that belong to the processed column.

The following example exports Grid data to PDF and customizes output table appearance:

@rendermode InteractiveServer
@using DevExpress.Drawing;
@inject WeatherForecastService ForecastService

<DxGrid @ref="Grid" Data="@forecasts">
    <Columns>
        <DxGridDataColumn Caption="Date" FieldName="Date" />
        <DxGridDataColumn Caption="Summary" FieldName="Summary" />
        <DxGridDataColumn Caption="Temperature (C)" FieldName="TemperatureC" />
        <DxGridDataColumn Caption="Temperature (F)" FieldName="TemperatureF" Visible="false" />
    </Columns>
    <ToolbarTemplate>
        <DxToolbar>
            <DxToolbarItem Text="Export to PDF" Click="ExportPdf_Click" BeginGroup="true" />
        </DxToolbar>
    </ToolbarTemplate>
</DxGrid>

@code {
    IGrid Grid;
    private WeatherForecast[]? forecasts;

    protected override async Task OnInitializedAsync() {
        forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
    }
    async Task ExportPdf_Click() {
        await Grid.ExportToPdfAsync("ExportResult", new GridPdfExportOptions() {
            FitToPage = false,
            CustomizeColumn = OnCustomizeColumn,
        });
    }
    void OnCustomizeColumn(GridDocumentExportCustomizeColumnEventArgs args) {
        args.Width = 400;
        args.IsHidden = false;
        if (args.ColumnIndex % 2 == 1)
            args.DataCellStyle.BackColor = System.Drawing.Color.LightGray;
        if (args.FieldName == "Date")
            args.DataCellStyle.Font = new DXFont(args.DataCellStyle.Font, DXFontStyle.Bold);
    }
}
csharp
using System;

public class WeatherForecast {
    public DateTime Date { get; set; }
    public int TemperatureC { get; set; }
    public string Forecast { get; set; }
    public string CloudCover { get; set; }
}
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

public class WeatherForecastService {
    private List<WeatherForecast> Forecast { get; set; }

    private static string[] CloudCover = new[] {
        "Sunny", "Partly cloudy", "Cloudy", "Storm"
    };

    Tuple<int, string>[] ConditionsForForecast = new Tuple<int, string>[] {
        Tuple.Create( 22 , "Hot"),
        Tuple.Create( 13 , "Warm"),
        Tuple.Create( 0 , "Cold"),
        Tuple.Create( -10 , "Freezing")
    };

    public WeatherForecastService() {
        Forecast = CreateForecast();
    }

    private List<WeatherForecast> CreateForecast() {
        var rng = new Random();
        DateTime startDate = DateTime.Now;
        return Enumerable.Range(1, 30).Select(index => {
            var temperatureC = rng.Next(-10, 30);
            return new WeatherForecast {
                Date = startDate.AddDays(index),
                TemperatureC = temperatureC,
                CloudCover = CloudCover[rng.Next(0, 4)],
                Forecast = ConditionsForForecast.First(c => c.Item1 <= temperatureC).Item2
            };
        }).ToList();
    }

    public List<WeatherForecast> GetForecast() {
        return Forecast;
    }

    public Task<WeatherForecast[]> GetForecastAsync(CancellationToken ct = default) {
        return Task.FromResult(Forecast.ToArray());
    }
}
csharp
// ...
builder.Services.AddSingleton<WeatherForecastService>();

Refer to the following topic for additional information: Export Blazor Grid Data to PDF.

See Also

GridDocumentExportOptions Class

GridDocumentExportOptions Members

DevExpress.Blazor Namespace