Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 WebGrid - Conditional Column Formatting

Is there anyway to do conditional formatting with Webgrid in ASP.NET MVC 3?

I know I can say: ... grid.Column("PropertyName", "Header Name", style: "bold") ...

and it will render HTML that for the TD that says: class="bold".

What I want, is to render some TDs in one style and other TDs in another style. Like: ... grid.Column("PropertyName", "Header Name", style: (item) => (item.Property > 100) ? "bold" : "normal")) ....

but this causes the error "Best overloaded method match ... has some invalid arguments."

Any idea if this is possible?

Thanks .Jim Biddison

like image 727
JBiddison Avatar asked Dec 20 '10 22:12

JBiddison


2 Answers

I know I'm kind of late with the answer, but if someone is still looking for that kind of conditional formating / column value binding for WebGrid here's somehting that works :

@grid.GetHtml(
   columns: grid.Columns(
      grid.Column(format: (item) => (item.someproperty !=null) ? 
        Html.Raw("I've got value") : 
        Html.Raw("I don't :("))
   )
)
like image 63
Paweł Staniec Avatar answered Nov 01 '22 18:11

Paweł Staniec


You can do this with some JQuery:

<script type='text/javascript'>
    $(document).ready(function () {
        jQuery.each($('tbody tr td'), function () {
            if (this.textContent == "some value") {
                $(this).addClass("some class");
            }
        });
    });
</script>

Of course, you'll have to modify the logic inside the each loop...

Hope that helps.

like image 5
camainc Avatar answered Nov 01 '22 19:11

camainc