This is a migrated thread and some comments may be shown as answers.

Blazor-TelerikGrid with Field-Binding that is kind of Dictionary property of the Main Object

3 Answers 1942 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 19 May 2020, 02:00 PM

Hello,

I need to migration some legacy stuff and some weird Object-constellation is giving me some problems with the binding in the Blazor Telerik-Grid.
Maybe some-one can give me some advise.

Example Code of legacy data structure:

class MainObject() {
    string Id {get; set;}
    SubObject[] SubObject {get;set;}
}
 
SubObject {
   string Key {get;set;}
   string Value {get;set;
}

 

Details: the SubObject contains some-kind of descriptive Information that is customized for every system.

Goal: 

protected IList<MainObject> MainObjectList{ get; set; }

 

<TelerikGrid Data=@MainObjectList>
<GridColumn Field="Id" Title="Id"></GridColumn>
<GridColumn Field="SubObject.First().Value" Title="SubObject.First().Key"></GridColumn>

 

What I try to achieve is to show some of the normal properties of the MainObject and some of the Descriptive-Informations inside the SubObjects in the Grid.
I did not even managed to show any of the Informations of the SubObject, is this even possible?

3 Answers, 1 is accepted

Sort by
0
Accepted
Marin Bratanov
Telerik team
answered on 19 May 2020, 02:13 PM

Hello Michael,

I can suggest you start by reviewing the following article (and especially the Notes section at the end) to see how the grid works with data binding: https://docs.telerik.com/blazor-ui/components/grid/columns/bound. The key thing is that the field the grid binds to must be readily readable and comparable. A collection (list, array, dictionary,...) is not, there is no built-in method that will let .NET and the Grid know what you want to do with such a collection, so it is not sortable, groupable, filterable, and there is no way to know what to render for it. It's even likely to throw exceptions.

That said, what you can do is to:

  1. remove the Field from this column (which will prevent it from throwing errors, and from being filterable, sortable, groupable)
  2. use the column template to show the desired item from the current model in the desired way

Of course, another approach is to flatten the data so the model that you bind the grid to only has a simple string (or int) field that you want to show.

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Michael
Top achievements
Rank 1
answered on 19 May 2020, 03:01 PM

Thanks for the input!

I will try what works best for my scenario.

0
Michael
Top achievements
Rank 1
answered on 20 May 2020, 05:12 AM

The Template did the trick! 

With following code I am even able to use Linq expressions to just show the specific fields configured externally:

<GridColumns>
@foreach(var field in FieldsToShow)
{
<GridColumn Field="Id" Title="@field.Label">
<Template>
@((context as MainObject).SubObjects.(First)(entry => entry.Key == field.Key).Value)
</Template>
}
</GridColumns>

I still lost the possibility to sort, group and filter because my actual model is messed up but these features are in current state not needed so should be good enough for my use case.
If they will be relevant later I will follow your other suggestions.

Thanks again!

Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Michael
Top achievements
Rank 1
Share this question
or