MvcContrib grid conditional cell formatting based on model value

590 views
Skip to first unread message

Rodrigo Juarez

unread,
Jan 23, 2011, 11:25:43 AM1/23/11
to mvccontrib-discuss
Hi

I need to conditional formatting a cell value based on a boolean value
in the model.
I have the column col.For(item => item.Detail); If item.Unfinished I
need to apply some css style
How can I do that?

Jeremy Skinner

unread,
Jan 23, 2011, 11:36:40 AM1/23/11
to mvccontri...@googlegroups.com
You can use the Attributes method to do this. There are several overloads for this method, but one takes a lambda expression that can be used to determine which item is being rendered and returns a dictionary of attributes. Here's an example:

column.For(x => x.Surname).Attributes(x => {
  if(x.Item.Surname == "foo") {
    return new Dictionary<string
  }
});



--
Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project.  Or go to http://mvccontrib.org

To unsubscribe from this group, send email to mvccontrib-disc...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mvccontrib-discuss?hl=en

Jeremy Skinner

unread,
Jan 23, 2011, 11:37:26 AM1/23/11
to mvccontri...@googlegroups.com
Whoops, pressed send too quickly. Here's the full example:

column.For(x => x.Surname).Attributes(x => {
  if(x.Item.Surname == "foo") {
    return new Dictionary<string, object> { { "style", "color:red"} };
  }
  return new Dictionary<string, object>();
});

Rodrigo Juarez

unread,
Jan 23, 2011, 3:55:19 PM1/23/11
to mvccontrib-discuss
Thanks a lot!

It's working, but intellisense doesn't work in if(x.Item.Surname ==
"foo") {
I can't see the Item when I press x.
I'm using visual studio 2010 and resharper 5.1
I should add some reference?



On Jan 23, 1:37 pm, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
> Whoops, pressed send too quickly. Here's the full example:
>
> column.For(x => x.Surname).Attributes(x => {
>   if(x.Item.Surname == "foo") {
>     return new Dictionary<string, object> { { "style", "color:red"} };
>   }
>   return new Dictionary<string, object>();
>
> });
>
> On 23 January 2011 16:36, Jeremy Skinner <jer...@jeremyskinner.co.uk> wrote:
>
>
>
> > You can use the Attributes method to do this. There are several overloads
> > for this method, but one takes a lambda expression that can be used to
> > determine which item is being rendered and returns a dictionary of
> > attributes. Here's an example:
>
> > column.For(x => x.Surname).Attributes(x => {
> >   if(x.Item.Surname == "foo") {
> >     return new Dictionary<string
> >   }
> > });
>
> > On 23 January 2011 16:25, Rodrigo Juarez <consul...@rodrigojuarez.com.ar>wrote:
>
> >> Hi
>
> >> I need to conditional formatting a cell value based on a boolean value
> >> in the model.
> >> I have the column col.For(item => item.Detail); If item.Unfinished I
> >> need to apply some css style
> >> How can I do that?
>
> >> --
> >> Contact Jeffrey Palermo or Eric Hexter with specific questions about the
> >> MvcContrib project.  Or go tohttp://mvccontrib.org
>
> >> To unsubscribe from this group, send email to
> >> mvccontrib-disc...@googlegroups.com
> >> For more options, visit this group at
> >>http://groups.google.com/group/mvccontrib-discuss?hl=en- Hide quoted text -
>
> - Show quoted text -

Jeremy Skinner

unread,
Jan 23, 2011, 3:59:04 PM1/23/11
to mvccontri...@googlegroups.com
No idea why this wouldn't show up in intellisense. It should work just fine at runtime though.

Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project.  Or go to http://mvccontrib.org
Reply all
Reply to author
Forward
0 new messages