If else in Web Grid Column

Anup picture Anup · Dec 24, 2013 · Viewed 21.6k times · Source

How to put a condition (if else) in webgrid column?

@grid.GetHtml(tableStyle: "table table-bordered",
                columns: grid.Columns(
                grid.Column("RealName", "Name"),
                grid.Column("UserName", "Email")
                ))

I have to show the Email Column based on a condition, How to do this?

Answer

Nilesh Gajare picture Nilesh Gajare · Dec 24, 2013

You can try this

@{
    var gridColumns = new List<WebGridColumn>();
    gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Select", "Details")));
    if (true)
    {
        gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Edit", "Edit")));
    }

    gridColumns.Add(grid.Column("UserName", "name"));
    gridColumns.Add(grid.Column("RealName", "RealName"));
}

@grid.GetHtml(columns: grid.Columns(gridColumns.ToArray()));