Search code examples
kendo-gridkendo-asp.net-mvc

How to hide one column based on condition in kendo grid


Below is my kendo grid in this grid I need to hide Case Number column conditionally that means if(admin == true) I need to show this column or else I need to hide this column how can I do this

@(Html.Kendo().Grid(Model.GiIncidentReportList)
.Name("IRGrid").Columns(columns => {
  columns.Bound(r => r.IncidentReport).Title("Case Number");
  columns.Bound(r => r.IncidentCreatedByName).Title("Created By");
  columns.Bound(r => r.IncidentCreatedDateTime).Title("Created Date");
  columns.Bound(r => r.IncidentUpdatedByName).Title("Updated By");
  columns.Bound(r => r.IncidentUpdatedDateTime).Title("Updated Date");
  columns.Template(p => 
    @Html.ActionLink("Delete","DeleteIncidentReport","IncidentReport",
                     new { incidentReportId = p.IncidentReport.IR_IncidentID, dlLogId = p.IncidentReport.DL_LogID, incidentType = p.IncidentReport.IT_IncidentType }, 
                     new { @class = "k-button k-button-icontext", onclick = "return confirm('Are you sure you wish to delete this report?')" }).ToHtmlString()

    );
  })
)

What I tried

if(admin == true){
  var grdView = $('#IRGrid').data('kendoGrid');
  grdView.hideColumn("IncidentReport"); //By Using Columns Name.
}

It is working but I want to handle the show and hide at columns.bound only instead of using if condition.


Solution

  • You can pass the Value through @Viewbag and give the condition like this

    @(Html.Kendo().Grid(Model.GiIncidentReportList)
    .Name("IRGrid").Columns(columns => {
     if (@ViewBag.admin == "True")
     {
      columns.Bound(r => r.IncidentReport).Title("Case Number");
      }
      columns.Bound(r => r.IncidentCreatedByName).Title("Created By");
      columns.Bound(r => r.IncidentCreatedDateTime).Title("Created Date");
      columns.Bound(r => r.IncidentUpdatedByName).Title("Updated By");
      columns.Bound(r => r.IncidentUpdatedDateTime).Title("Updated Date");
      columns.Template(p => 
        @Html.ActionLink("Delete","DeleteIncidentReport","IncidentReport",
                         new { incidentReportId = p.IncidentReport.IR_IncidentID, dlLogId = p.IncidentReport.DL_LogID, incidentType = p.IncidentReport.IT_IncidentType }, 
                         new { @class = "k-button k-button-icontext", onclick = "return confirm('Are you sure you wish to delete this report?')" }).ToHtmlString()
    
        );
      })
    )