Source
Here on check of a condition I am hiding my two columns Password and Email Address. By doing so, I can prevent my webgrid to show the column which I dont want to display for a particular condition.
var grid = new WebGrid(Model.User)
if(myConditionCanGoInHere) {
@grid.GetHtml(columns: grid.Columns(
grid.Column("Name"),
grid.Column("Password"),
grid.Column("EmailAddress") ))
}
else{
@grid.GetHtml(columns: grid.Columns(
grid.Column("Name")
// Here I have not included Password and EmailAddress columns.
))
}
This is when you want to hide the full column, but what if you want to show the column and hide only specific values, then in that case what is to be done ? That too can be done, you can have a if else condition like the one showed below…
@grid.GetHtml(
columns: grid.Columns(
grid.Column(header: "Edit", format: item => item.CanEdit ? Html.ActionLink("Edit","Edit","Person",new {id = item.PersonId},null) : Html.Raw("uneditable")),
grid.Column(columnName: "CanEdit", header: "Editable?"),
grid.Column(columnName: "DisplayName", header: "Display Name"),
grid.Column(columnName: "Email", header: "Email")
)
)