DataGrid with Autocolumn and overwrite

1 Answer 20 Views
Grid
Serkan
Top achievements
Rank 1
Serkan asked on 06 Mar 2025, 04:26 PM | edited on 06 Mar 2025, 04:46 PM

Hello!

i use Entity Framework and my class has over 150 attibutes. So i use AutogenerateColums.

I want overwrite the value with template for example: Inventory.IsFragmented is boolean.

So in the Grid i dont want show true or false. i want text: Ja / Nein.

But my code show only true or false. Have someone an idea how can i do it. Thank you :)

 


<TelerikGrid Data="@GridData" @ref="@Grid" Sortable="true" Reorderable="true" Resizable="true" Pageable="@true" AutoGenerateColumns ="true" ShowColumnMenu="true" FilterMode="@GridFilterMode.FilterMenu" OnRowDoubleClick="@OnRowDoubleClickHandler" SelectionMode="GridSelectionMode.Multiple" > <GridToolBarTemplate> <GridCommandButton OnClick="@AutoFitAllColumns" Icon="@FontIcon.ColResize" Title="Spalten-Autoanpassung" Class="tooltip-target"></GridCommandButton> <GridCommandButton Command="ExcelExport" Icon="@SvgIcon.FileExcel">Export to Excel</GridCommandButton> <GridCommandButton Command="CsvExport" Icon="@SvgIcon.FileCsv">Export to CSV</GridCommandButton> <label class="k-checkbox-label"><TelerikCheckBox @bind-Value="@ExportAllPages" />Alle Seiten exportieren</label> </GridToolBarTemplate> <GridColumns> <GridCheckboxColumn></GridCheckboxColumn> <GridAutoGeneratedColumns ColumnWidth="200px" /> </GridColumn> <GridColumn Field=@nameof(Inventory.IsFragmented)> <Template > @{ var x = context as Inventory; var b = x.IsFragmented == true ? "Ja" : "Nein"; } <p> @(x.IsFragmented ? "Ja" : "Nein")</p> </Template> </GridColumn> <GridColumn Field=@nameof(Inventory.Immobile) Visible="true"> <Template> @{ var x = context as Inventory; if (x.Immobile == true) { <p>Ja</p> } else { <p>Nein</p> } }

<p>NEIN</p> </Template> </GridColumn> </GridColumns> <GridExport> <GridExcelExport FileName="ve-excel-export" AllPages="@ExportAllPages" /> <GridCsvExport FileName="ve-csv-export" AllPages="@ExportAllPages" /> </GridExport> </TelerikGrid>


1 Answer, 1 is accepted

Sort by
1
Accepted
Anislav
Top achievements
Rank 4
Bronze
Bronze
Iron
answered on 06 Mar 2025, 08:10 PM | edited on 06 Mar 2025, 08:11 PM
Hi Serkan,

When using AutoGenerateColumns="true", the grid creates columns for all properties, including IsFragmented and Immobile. This results in the default boolean values (true/false) being displayed instead of your custom "Ja/Nein" text.

To fix this, add the [Display(AutoGenerateField = false)] attribute to these properties in your model. This prevents auto-generation of columns from these fields while allowing your manually defined column templates to display "Ja/Nein" correctly.

public class Inventory
{
    [Display(AutoGenerateField = false)]
    public bool IsFragmented { get; set; }
    
    [Display(AutoGenerateField = false)]
    public bool Immobile { get; set; }
}


Regards,
Anislav Atanasov
Serkan
Top achievements
Rank 1
commented on 08 Mar 2025, 07:48 AM

Thank you Anislav.

That is it. So it works. Thank you.

Tags
Grid
Asked by
Serkan
Top achievements
Rank 1
Answers by
Anislav
Top achievements
Rank 4
Bronze
Bronze
Iron
Share this question
or