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

Alternate Image in Grid Cell depending on Value in Next Column

1 Answer 37 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Roger
Top achievements
Rank 2
Veteran
Roger asked on 30 Aug 2012, 11:25 PM
Hi:

I have a grid that I want to show check marks or x marks depending on whether a student has got a question right or wrong.  

The relevant columns in my grid are:

<telerik:GridBoundColumn DataField="IsCorrect" FilterControlAltText="Filter IsCorrect column"
                        HeaderText="IsCorrect" SortExpression="IsCorrect" UniqueName="IsCorrect" DataType="System.Int32">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn UniqueName="CheckMark" HeaderText="Status">
                        <ItemTemplate>
                            <asp:Image ID="TemplateColumn" runat="server" ImageUrl="~/Images/check2.gif" />
                        </ItemTemplate>
                        <ItemStyle Width="30px" />
                        <HeaderStyle Width="30px" />
                    </telerik:GridTemplateColumn>

Currently, I am able to show check marks by overriding the check mark with blank text - i.e.

protected void grdExamReults_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
         {
             GridDataItem dataItem = e.Item as GridDataItem;
 
             try
             {
                 if (dataItem["IsCorrect"].Text != "1")
                     dataItem["CheckMark"].Text = "";
             }
             catch
             {
             }
         }
However, what I would really like to do is to show an xmark (wrong.gif) if dataItem["isCorrect].Text !="1".

Any help on this greatly appreciated.

RBS

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 31 Aug 2012, 04:34 AM
Hi,

Try the following code to achieve your scenario.
C#:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
  GridDataItem item = (GridDataItem)e.Item;
  if (item["IsCorrect"].Text != "1")
  {
    Image img = new Image();
    img.ImageUrl = "~/Images/wrong.gif";
    item["IsCorrect"].Controls.Add(img);
  }
 }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Roger
Top achievements
Rank 2
Veteran
Answers by
Shinu
Top achievements
Rank 2
Share this question
or