If-condition in a DataGrid column template

I am new to the Radzen and Razor world, so I am struggling with the first steps.
I would like to transform a boolean to "yes" or "no".

In the Radzen docs, I don't see an example with C# code inside a template.
I have an attribute called "IsActive". So I tried the following in the "Template" property of my column:

@if(data.IsActive)(<span>Yes</span>)
and
@if(data.IsActive)(@(β€œYes”))
--> : A namespace cannot directly contain members such as fields, methods or statements

@if(${data.IsActive})({"Yes"})
--> The type or namespace name 'MyPage' could not be found (are you missing a using directive or an assembly reference?)

$if{data.IsActive ?? false}{"Yes"}
--> this writes the whole string in raw

Just to try out any code related output:
@Html.Raw("Test")
--> The name 'Html' does not exist in the current context

Thank you very much for your help getting started!

Hi @rene,

You can check the Expressions article: Expressions (Blazor)

You can use the following in the Template:

${data.IsActive ? "yes" : "no"}
2 Likes

Thank you for your quick answer, this was exactly what I needed to know to get it right:
first it was complaining, that it could be null as well:

Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

This was my final solution:

${(data.IsActive ?? false) ? "yes" : "no"}

This would work with bool? too:

${data.IsActive == true ? "yes" : "no"}
1 Like