Grid column hiding in codebehind?

1 Answer 20 Views
Grid
Deasun
Top achievements
Rank 3
Bronze
Iron
Iron
Deasun asked on 15 Feb 2023, 01:45 PM

I have a grid:

 <TelerikGrid @ref="@gridHDR"
                             Data="@GDgridHDR"
                             AutoGenerateColumns="true"
                             Pageable="true"
                             Sortable="true"
                             Class="custom-row-colors"
                             FilterMode="@GridFilterMode.FilterRow">
                        <GridToolBarTemplate>
                                <GridCommandButton Command="ExcelExport" Icon="@FontIcon.FileExcel">Export to Excel</GridCommandButton>
                            <label class="k-checkbox-label"><TelerikCheckBox @bind-Value="@gridHDR_ExportAllPages" /> Export All Pages</label>
                            </GridToolBarTemplate>
                            <GridExport>
                            <GridExcelExport FileName="@msExportFileName" AllPages="@gridHDR_ExportAllPages" OnBeforeExport="@gridHDR_OnBeforeExcelExport" />
                            </GridExport>                        
                        </TelerikGrid>

It autogenerates the columns .

I have a need to hide one or more of the columns if the user checks off a checkbox on the UI.

So i would like to do something like:

Grid.Column[0].visible = False;

Is there a way to do this without me having to put in all the Column tags?

If I do have to add in the column tags can I only add in the ones that may be turned off/On or do I have to add in all the columns coming back?

Hopefully that makes some sense.

Thanks

Deasun

Deasun
Top achievements
Rank 3
Bronze
Iron
Iron
commented on 16 Feb 2023, 02:11 PM

This might help with what I am trying to do:

<TelerikGrid Data="@MyData" Pageable="true" PageSize="10"> <GridColumns> <GridColumn Field="@(nameof(SampleData.Id))" Width="120px" /> <GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" /> <GridColumn Field="@(nameof(SampleData.Team))" Title="Team" /> <GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" Visible="false" /> </GridColumns> </TelerikGrid>

 

Is there any way to do that with AutoGenerate columns option enabled?

 

Thanks

Deasun.

1 Answer, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 20 Feb 2023, 07:05 AM

Hello Deasun,

The following syntax is not the Blazor way. It is called direct parameter mutation and is not recommended. It usually doesn't work anyway:

Grid.Column[0].visible = False;

There are two ways to hide Grid columns:

  • Use declared columns and set the Visible parameter.
  • Use autogenerated columns and the Display DataAnnotations attrbute:

@using System.ComponentModel.DataAnnotations

<TelerikGrid Data="@GridData"
             AutoGenerateColumns="true">
</TelerikGrid>

@code {
    List<GridModel> GridData { get; set; } = new List<GridModel>()
    {
        new GridModel() { Id = 1, Text = "Text 1" }
    };

    public class GridModel
    {
        [Display(AutoGenerateField = false)]
        public int Id { get; set; }
        public string Text { get; set; }
    }
}

Regards,
Dimo
Progress Telerik

Learn about the important changes coming in UI for Blazor 4.0 in January 2023!
Tags
Grid
Asked by
Deasun
Top achievements
Rank 3
Bronze
Iron
Iron
Answers by
Dimo
Telerik team
Share this question
or