This is a migrated thread and some comments may be shown as answers.

[Solved] Changing the value of a cell

13 Answers 190 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
william
Top achievements
Rank 1
william asked on 31 Aug 2011, 10:38 AM
Hi All,

I currently have a column that is bound to a boolean dataitem :

columns.Bound(r => r.BDN).Width(45);

At the moment the cells for this item show the words 'true' or 'false' based on the underlying value. What I really want to do is when the underlying value is false show an image of a cross in the cell and when true show an image of a tick.

I thought I'd be able to do something like:
.CellAction(cell => { if (cell.DataItem.BDN == false) { cell.Content = "url to my image"; } })
However I receive an error that the value I'm trying to set in the cell doesn't corespond to the underlying data type.

13 Answers, 1 is accepted

Sort by
0
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2011, 01:04 PM
You can use the ClientTemplate method like this:

.ClientTemplate("<#= BDN?<img src='urlTick' /> : <img src='urlCross' />#>")
0
william
Top achievements
Rank 1
answered on 31 Aug 2011, 01:46 PM
Hi,

I have replaced my original binding line with:

columns.Bound(r => r.BDN).Width(45).ClientTemplate("<#= BDN?<img src='" + Url.Content("~/Content/tick.jpg") + "'/> : <img src='" + Url.Content("~/Content/cross.jpg") + "'/>#>").Encoded(false);

but now I get a break in the jQuery library when I run the code.
0
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2011, 02:24 PM
Sorry, my mistake, adapting it to your example it would be like this:
columns.Bound(r => r.BDN).Width(45).ClientTemplate("<img src='<#= BDN?'" + Url.Content("~/Content/tick.jpg") + "' : '" + Url.Content("~/Content/cross.jpg") + "'#> />");

The problem was that I had a typo, the single quote before and after each img tag were missing, though this adaption is shorter to write than the other one
0
william
Top achievements
Rank 1
answered on 31 Aug 2011, 02:45 PM
Hi Thanks for your help so far.

I have attached 2 images of my screen. before is before using the clientTemplate and After is using the ClientTemplate. THe images exist in the URL.ContentLocation. and it now only shows the first record.
0
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2011, 02:47 PM
You have to apply this ClientTemplate anywhere you want to show the images, just taking into account you will have to change the
<#= BDN
part with:
<#= PropertyName

For example, the second column would be:
.ClientTemplate("<img src='<#= EXP?....

As for the image issue try using:

Page.ResolveClientUrl("~/Content/tick.jpg") and Page.ResolveClientUrl("~/Content/cross.jpg") instead of Url.Content and let me know if it works
0
william
Top achievements
Rank 1
answered on 31 Aug 2011, 02:54 PM
at the moment I'm just concentrating on the BDN column. When I use the client template my second row of data does not get displayed and the url image for the record that does display, doesn't show.

to prove I have the url right, if I change the column to:

columns.Bound(r => r.BDN).Width(45).Format("<img src='" + Url.Content("~/Content/Tick.jpg") + "'/>").Encoded(false); }

I recieve the tick in the cell, but of course this isn't good enough as it doesn't do the test for true or false.
0
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2011, 02:56 PM
Have you tried this?

As for the image issue try using:

Page.ResolveClientUrl("~/Content/tick.jpg") and Page.ResolveClientUrl("~/Content/cross.jpg") instead of Url.Content and let me know if it works


I posted this after replying before so maybe you did not read it.
0
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2011, 03:01 PM
Do you have developer tools of IE installed or firebug in Firefox so you can check what src has the img element?
0
william
Top achievements
Rank 1
answered on 31 Aug 2011, 03:13 PM
using the Page.ResolveClientUrl when I attempt to go to my page I receive the following error:

Cannot perform runtime binding on a null reference in the IDE.

I do have IE Developer Tools, but because I cannot get onto the page due to the above error, I cannot see what's in it
0
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2011, 03:14 PM
Then leave it as you had before Url.Content... and check the src to see what it has
0
william
Top achievements
Rank 1
answered on 31 Aug 2011, 03:22 PM
Yes, I have reverted back.

I only have the first line of data showing in the grid ???

However, looking at the line I have got, for the image it looks like this:

<img src="/Content/Cross.jpg />

So the ~ is missing along with closing quote.
0
Accepted
Nohinn
Top achievements
Rank 1
answered on 31 Aug 2011, 03:26 PM
Silly me again haha, I should ask for your pardon.

There's another typo in the clienttemplate code

after the #> closing tag write a single quote mark, so:

#>' />")
0
william
Top achievements
Rank 1
answered on 31 Aug 2011, 03:30 PM
Woho.....Thanks very much for all your effort. It's all working. Now I can do the same with the other columns.
Tags
Grid
Asked by
william
Top achievements
Rank 1
Answers by
Nohinn
Top achievements
Rank 1
william
Top achievements
Rank 1
Share this question
or