8

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

4 Answers 4

9

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

1 Comment

this is conditional text, but not conditional formatting/styling. i think he's trying to change the style
5

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.

Comments

0

I don't think the style property accepts functions. You can use jQuery or here is a hack:

Hack

Comments

0

For googler, an improved version of the Torm answer:

@grid.GetHtml(
    columns: new[]
    {       
        grid.Column(format:item => Html.Raw("<span" + (item.Property > 100 ? " style='bold'" : "") + ">" + item.Property + "</span>")),
    }
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.