If else in Web Grid Column
Asked Answered
Z

5

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?

Zippel answered 24/12, 2013 at 10:1 Comment(3)
Try this may be Duplicate of [this link][1] [1]: #7866551Naive
I have to show / hide column based on a condition, The above thread shows or hides the column value, so its not duplicate.Zippel
then modify your question to if else for WebGrid columnNaive
N
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()));
Naive answered 24/12, 2013 at 10:50 Comment(1)
Thank you @Nilesh, looks like the cleanest way of doing thisBestiality
B
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)
))
Bridgeman answered 16/1, 2014 at 13:28 Comment(0)
G
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

Gitt answered 24/12, 2013 at 10:24 Comment(2)
thankx for answer, but how do i put entire column in condition. So if condition is true then the column displays & vice versa.Zippel
@Zippel Check the updated answer it's a very basic answer check if it works for you .Gitt
M
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)
Maximomaximum answered 19/2, 2014 at 23:38 Comment(0)
O
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

Outdistance answered 24/12, 2013 at 10:27 Comment(4)
I am looking for quick fix buddy.Zippel
Then look at this one :) @ZippelOutdistance
Well i cannot change in controller, since it is being used by multiple users.Zippel
Klick on "This" in my last answer... There you don't have to change the Controller...Outdistance

© 2022 - 2024 — McMap. All rights reserved.