Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If else in Web Grid Column

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?

like image 849
Anup Avatar asked Dec 24 '13 10:12

Anup


2 Answers

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()));
like image 138
Nilesh Gajare Avatar answered Oct 22 '22 11:10

Nilesh Gajare


This worked for me.

 @grid.GetHtml(tableStyle: "webGrid",
        headerStyle: "header",
        alternatingRowStyle: "alt",
        selectedRowStyle: "select",
        columns: grid.Columns(




        grid.Column("Is Active",format: (item) =>
            {
                if (item.IsActive == true)
                {
                    return Html.Raw(string.Format("<text><img height='20' width='20' src=\"{0}\" alt=\"Image\"/></text>", Url.Content("~/images/rightmark.png")));
                }
                else
                {
                    return Html.Raw(string.Format("<text><img height='20' width='20' src=\"{0}\" alt=\"Image\"/></text>", Url.Content("~/Content/images/non-preview-photo.gif")));                         
                }
            }, style: "firstColumn",canSort:true),       
        grid.Column("Name", " Name", style: "SecondColumn",canSort:true),       
        grid.Column("Role", "Role", style: "ThirdColumn",canSort:true)
))
like image 43
Ajay Kumar Avatar answered Oct 22 '22 11:10

Ajay Kumar