1

Can anyone please advise how I can set the date value in a row to "" if the source data is a null date (01/01/0001)

I have tried this way with no luck..

<MudTd DataLabel="EndDate" Style="width: 160px">@row.EndDate == @null ? '' : @Convert.ToDateTime(row.EndDate).ToString("dd/MM/yyyy")</MudTd>

Regards Peter

1 Answer 1

2

For that syntax to work (using the conditional operator), you need to wrap the whole expression in script block, this is the same rule for string interpolation:

<MudTd DataLabel="EndDate" 
       Style="width: 160px">
    @(row.EndDate == null ? "" : Convert.ToDateTime(row.EndDate).ToString("dd/MM/yyyy"))
</MudTd>

Given that you are casting to a date, is the value even null in the first place? if it is a string, try IsNullOrEmpty / IsNullOrWhiteSpace ...

<MudTd DataLabel="EndDate" 
       Style="width: 160px">
    @(String.IsNullOrWhiteSpace(row.EndDate) ? "" : Convert.ToDateTime(row.EndDate).ToString("dd/MM/yyyy"))
</MudTd>

If the value is a nullable DateTime? then do not use Convert.ToDateTime or conditional operator (in-line if expression), just use String.Format which will implicitly convert a null value to an empty string:

<MudTd>@String.Format("{0:dd/MM/yyyy}", @row.EndDate)</MudTd>

Try this in MudBlazor here: https://try.mudblazor.com/snippet/QkcRFPmgFqxqgoRW

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh i see, I'll try that thanks, and yes is a null value not a string so it gets pulled in a a null date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.