Replace values in Blazor Datagrid

Hi, i use the RadzenDataGrid in a Blazor Wasm project.
The data is from a service (odata) generated from BlazorStudio. All works perfect.
In the table are several columns that hold Fk-values (all integers).
They map to lookup-tables with 4 or five entries that never changed (for ex. table 'salutation' --> 1='Ms.', 2='Mr.', ...) and there are no real FK-definitions in the database (all handled in the Access-VBA-code)!!
Since it is an existing project, i can not change the database structure.
But i need to show the 'values' (mostly strings) not the key (integer) in the grid column!
So my idea: Add a column with a switch that replace the column:


@switch (tMandantens.Anrede)
{
case 1:
"Frau";
break;
case 2:
"Herr";
break;
default:
" ";
break;
}

Problem: How can i create/add such an RadzenDataGridColumn that not exists in the 'TItem' data structure (this is also necessary for 'calculated columns')?

Where is the best place, to do the 'replacement'? On client-side if the data is visualized in the grid? On server-side if the data is read from the database?

Thanks for help.

Patric

You don't need to set the Property attribute of such columns - just set their template. You can use a dictionary to map those values. Something like this:

<RadzenDataGridColumn TItem="MyItem">
     <Template Context="data">
          @dictionary[data.Anrede]
     </Template>
</RadzenDataGridColumn>

@code {
    Dictionary<int, string> dictionary = new Dictionary<int, string>
    {
       {1, "Frau"},
       {2, "Herr"}
    };
}

Hi Korchev,
thanks for your super fast reply.

My first working solution was:

  1. Add a column, but with a TItem to the TItem of the data grid; but no proberty-mapping
  2. Add a template with a context to the Data-object of the data grid
  3. Add a RadzenText with code; here i use a cascade of ?-operators



    Test: @((tMandantens.Mandantengruppe == 1) ? "Frau" :
    (tMandantens.Mandantengruppe == 2) ? "Herr" :
    (tMandantens.Mandantengruppe == 3) ? "keine Angabe" : "Fehlende Angabe")


But your solution with the Dictonary is better. I must change it a little since the people who create the table decside to make the FK as a nullable short (i hate them)!! So the final is:

@dM_Gruppe[((tMandantens.Mandantengruppe.Equals(null)) ? 0 : tMandantens.Mandantengruppe.Value)]

Do you have a recommendation if the key map 1 to 1 for ex. to an 'person table'; so i want to show the Person Name and the Phone number in a data grid cell? And there is no real FK-Mapping.

And where is the best place to map this information?

Thanks for your support

Patric

You can fetch the persons table and convert it to a dictionary (use the ToDictionary extension method for collections). Do that in the OnInitialized/Async and then use the dictionary in the template. Something like this:


protected IDictionary<int, string> people;

protected override void OnInitialized()
{
    people = myDbContext.People.ToDictionary(p => p.Id, p => p.Name);
}

Hi Korchev,

thanks for your ideas, it work.

One additional question:

I use the structure that BlazorStudio generated;

So there is a page with the dataGrid to show the data,
a page with a form to add data and a page with a form to change the data.

Each page can use the same dictionary. So where is the best place to create and store the dictonary or several dictonaries and how can i used them in several pages (like a global structure)?

Thanks for help.

Patric

You can add them as properties of the services that Radzen Blazor Studio has generated for your project. They are injected in the generated pages. Or create and register custom services.