6
votes

I am using Kendo ui grid in asp.net MVC. Is it possible to hide/show grid column based on user role? Thanks

2

2 Answers

10
votes

The simplest way is:

@(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.Id);
        columns.Bound(p => p.Name);
        if(User.IsInRole("Admin")) {
            columns.Bound(p => p.AdminOnlyInfo);
        }
    })
    ...
)
4
votes

You can specify if a column is visible using hidden, so one option may be to set a variable based on the users role. For example, in the controller

ViewBag.CanDisplay = true; // or omit if the user does not have permission

and in the view

var canDisplay = '@ViewBag.CanDisplay' | false;
$("#grid").kendoGrid({
  columns: [
    { field: "firstProperty" },
    { field: "anotherProperty", hidden: !canDisplay }    
  ],