Blazorise: DataGrid model parent property?

Created on 31 Jul 2019  Â·  14Comments  Â·  Source: stsrki/Blazorise

How can I display a parent property for a linked model?

Trying
"@nameof(Log.LogSource.SourceName)"
causes
ArgumentException: Instance property 'SourceName' is not defined for type 'proj.Shared.Models.Log' (Parameter 'propertyName')

My log model has a virtual property to a LogSource model. Does the data grid support this right now?

In Progress Feature âš™

All 14 comments

Currently DataGrid supports only access to "first-level" fields. The plan is to add access to linked model fields in the next version.

Blazorise 0.8 was already behind schedule so I didn't want to add too many features now since that would take me too much time. Next version will be smaller so this feature will probably not take me too long to do it.

Sounds good. Is wildcarding searching allowed in filter? And how do you format date properties?

For now just a simple "Contain" is used for search filter. I will add other filter types later.
All fields are formatted with default "ToString" based on there type. If you want custom format you can use DisplayTemplate. eg

<DataGridNumericColumn TItem="Employee" Field="@nameof(Employee.DateOfBirth)" Caption="Date Of Birth" AllowEdit="true">
    <DisplayTemplate>
        @{
            var date = ( context as Employee )?.DateOfBirth;

            if ( date != null )
            {
                @($"{date.Value.ToShortDateString()}, age: {( DateTime.Now.Year - date.Value.Year )}")
            }
        }
    </DisplayTemplate>
</DataGridNumericColumn>

Thanks, I'll give that a try.

I'm investigating a little how to do access to the nested properties.

The getting part of value is not the problem as that is fairly easy to build an expression tree, even when checking the null values.

What I'm not sure is what to do for the setter method. What I mean by that is this. If I want to write something to "Log.LogSource.SourceName", what do I do if LogSource is null? Do I just skip the write or do I create a new LogSource object? But if I initialize it then all the other properties and fields in that object will be "empty".

I think the best option is to skip write if any nested property is null. What do you think?

I agree, skipping it would be the right way to go.

Will be released with 0.8.1

Will be released with 0.8.1

This still doesn't work. Is there additional code I need to adjust? I"m getting the following error on 8.1:
ArgumentException: 'SourceName' is not a member of type 'Proj.XX.ZZ.Log' (Parameter 'propertyOrFieldName')
using this data grid column:
TItem="Log" Field="@nameof(Log.LogSource.SourceName)"

You need to remove the nameof because that would take only the last field name, in your case the SourceName.

For nested fields just write Field="Log.LogSource.SourceName"

I must be missing something else, that didn't help. Now I get:
ArgumentException: 'Log' is not a member of type 'Proj.XX.YY.Log' (Parameter 'propertyOrFieldName')

I cannot do much to test now as I'm on vacation for a weekend.

But, basically you just need to write a full path to the nested field as a string. If you dont manage to do it I can look at it after I get back.

Ok.. This is what I have:
<DataGridColumn TItem="Log" Field="Log.LogSource.SourceName" Caption="Log Source" /> That throws the last error.

Try to remove the Log, so leave only "LogSource.SourceName"

That was it. Thanks!

Was this page helpful?
0 / 5 - 0 ratings