How to get the value of GridForeignColumn when exporting to PDF?

 How to get the value of grid Foreign column when exporting to PDF? For example OrderID must be displayed as OrderName after exporting to PDF.

<GridColumns> <GridForeignColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" ForeignKeyValue="OrderName" TextAlign="TextAlign.Right" Width="120"></GridColumn> <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn> <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn> <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn> </GridColumns>



1 Reply

MS Monisha Saravanan Syncfusion Team May 17, 2022 06:44 AM UTC

Hi Belle Perez,


Greetings from Syncfusion support.


Query: “How to get the value of grid Foreign column when exporting to PDF? For example OrderID must be displayed as OrderName after exporting to PDF.”


We have checked your query and we suspect that you need to customize the grid header after it is exported. We would like to inform that we can customize PdfHeaderQueryCellInfoHandler to customize the Grid header. Here we have customized the header cell value inside PdfHeaderQueryCellInfoHandler. Kindly check the attached code snippet and sample for your reference.


 

<SfGrid ID="Grid" @ref="DefaultGrid" DataSource="@Orders" AllowFiltering="true" AllowSorting="true" Toolbar="@(new List<string>() { "ExcelExport", "PdfExport" })" AllowPdfExport="true" AllowExcelExport="true" AllowPaging="true">

    <GridEvents OnToolbarClick="ToolbarClickHandler" PdfHeaderQueryCellInfoEvent="PdfHeaderQueryCellInfoHandler" TValue="Order"></GridEvents>

    <GridColumns>

        <GridForeignColumn Field=@nameof(Order.EmployeeID) HeaderText="EmployeeName" ForeignKeyValue="FirstName" ForeignDataSource="@Employees" Width="150"></GridForeignColumn>

    </GridColumns>

</SfGrid>

 

@code{

    private SfGrid<Order> DefaultGrid;

 

    public List<Order> Orders { get; set; }

    public List<EmployeeData> Employees { get; set; }

 

    public async Task ToolbarClickHandler(Syncfusion.Blazor.Navigations.ClickEventArgs args)

    {

        if (args.Item.Id == "Grid_excelexport") //Id is combination of Grid's ID and itemname

        {

            await this.DefaultGrid.ExcelExport();

        }

        else if (args.Item.Id == "Grid_pdfexport")

        {

            await this.DefaultGrid.ExportToPdfAsync();

        }

    }

    public void PdfHeaderQueryCellInfoHandler(PdfHeaderQueryCellInfoEventArgs args)

    {

        if (args.Cell.Value == "EmployeeName")

        {

            args.Cell.Value = args.Column.ForeignKeyValue;

        }

    }

 

 

}


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DataGrid_Server-1669336876.zip


Reference: https://blazor.syncfusion.com/documentation/datagrid/pdf-export#grid-cell-customization-in-pdf-export


If the reported issue is not resolved then kindly share the below details to validate further from our end.


  1. Share us the entire Grid code snippet.
  2. Share us the video demonstration of the issue explaining your query.
  3. If possible share us the issue reproduceable sample.


The above-requested details will be very helpful for us to validate the reported query at our end and provide the solution as early as possible.


Regards,

Monisha


Loader.
Up arrow icon