6

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?

3
  • Try this may be Duplicate of [this link][1] [1]: stackoverflow.com/questions/7866051/… Commented Dec 24, 2013 at 10:31
  • I have to show / hide column based on a condition, The above thread shows or hides the column value, so its not duplicate. Commented Dec 24, 2013 at 10:33
  • then modify your question to if else for WebGrid column Commented Dec 24, 2013 at 10:35

5 Answers 5

15

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()));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Nilesh, looks like the cleanest way of doing this
13

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)
))

Comments

3

A very simple way is

if(myConditionCanGoInHere) {

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

}
else{

 @grid.GetHtml(tableStyle: "table table-bordered",
            columns: grid.Columns(
            grid.Column("RealName", "Name"),
            //grid.Column("UserName", "Email")
            ))
// Here remove your email column

))

Reference and Here

2 Comments

thankx for answer, but how do i put entire column in condition. So if condition is true then the column displays & vice versa.
@Anup Check the updated answer it's a very basic answer check if it works for you .
2

I update the code to accept parameter: (Razor View - Webmatrix)

      grid.Column("Unread",format: (item) =>
        {
            if (item.Unread == true)
            {
                return Html.Raw(string.Format("<text><a \"target=\"_blank\" href=\"ViewComments?bvnum={0}\"><img height='20' width='20' border='0' src=\"/images/new_comments.png\" alt=\"Image\"/></text>", @item.id));
            }
            else
            {
                return Html.Raw(string.Format("<text><a \"target=\"_blank\" href=\"ViewComments?bvnum={0}\"><img height='20' width='20' border='0' src=\"/images/comments.png\" alt=\"Image\"/></text>", @item.id));
            }
        }, canSort:true)

Comments

1

It would be good, if you validate this before you put the data in the GUI layer. You have to get the right data for the grid in your Controller. So you can only show the data in the grid and you don't have to mind if it is the right data because you have already validate it.

That mean you have to put the if/else in your controller not in your view.

        public JsonResult GetServiceGridData([DataSourceRequest]DataSourceRequest request)
        {
            var services = ModelTransformer.Transform(Repository.Instance.GetServices());
            foreach (var service in services)
            {
                var filterType = _filterTypes.FirstOrDefault(x => x.Id == service.FilterTypeId);
                service.FilterTypeName = filterType == null ? _filterTypeNoneName : filterType.Name;
            }
            return Json(services.ToDataSourceResult(request));
        }

Something like this for example

4 Comments

I am looking for quick fix buddy.
Then look at this one :) @Anup
Well i cannot change in controller, since it is being used by multiple users.
Klick on "This" in my last answer... There you don't have to change the Controller...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.