Search code examples
c#.netblazorblazor-webassemblyradzen

Set "visible" property in RadzenDataGrid column based on value of the entire property of the object


I have a radzen datagrid that receives a IEnumerable<T> and displays a set of columns

<RadzenDataGrid AllowSorting="true" AllowColumnReorder="true"  AllowMultiColumnSorting="true"  PageSize="40" AllowFiltering="true" FilterMode="FilterMode.Simple" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" Data="@FilteredList" TItem="Employee" AllowPaging="true" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true">
        <Columns>
            <RadzenDataGridColumn TItem="Employee" Property="EmployeeName" Title="Employee Name"  Visible="@ShowColumn" />
            <RadzenDataGridColumn TItem="Employee" Property="EmployeeNumber" Title="Employee Number" Sortable="false" Width="125px"  TextAlign="TextAlign.Center"/>
            <RadzenDataGridColumn TItem="Employee" Property="EmployeeType" Title="Employee Type" TextAlign="TextAlign.Right" />
        </Columns>
    </RadzenDataGrid>

(simplified datagrid for brevity)

Now, the IEnumerable<Employee> that the RadzenDataGrid component receives is not always the same, based on previous conditions, let's say it can receive one that has all EmployeeName in null or can have all EmployeeType in null, so, what I want to do is to set the bool value of visible property of RadzenDataGrid to be a depending if there are values in the property of the object that receives.

I have set the RadzenDataGridColumn with the property Visible="@ShowColumn" but I don't know how I should do the method so it will calculate this bool for each RadzenDataGridColumn and I'm not entirely sure if it should go on the component page, or the parent that calls the component.


Solution

  • In the @code section, at some point you need to populate FilteredList. I think thats the point where you can calculate that bool variable. Maybe with a simple LINQ line:

    {
        FilteredList = PopulateFromDataStore();
        var showName = FilteredList.Any(e => e.Name != null);
    }
    

    That way, if ANY of the employees has value on his Name property, you show that column.