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

Binding a picture to a radgrid row based on the text value inside that specific row

4 Answers 654 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Goat_
Top achievements
Rank 1
Goat_ asked on 12 Apr 2013, 09:50 AM
Hi guys,

i have just started my first job and i've been thrown into the deep end and have been asked to create a C# asp.net website for this company. i have only been using telerik for 2 weeks.

I have a radgrid with a couple of columns which gets populated from a database based on a linq query. I have a status column and the status can either be "Open" or "Closed", when the data is populated into the radgrid i want a picture be be shown in a row instead of the text value "Open" / "Closed"

How would i go about doing this ?
 

Thank you in advance
 

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 12 Apr 2013, 09:57 AM
Hi,

Try the following code.
c#:
void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            if (item["Status"].Text == "Open" || item["Status"].Text == "Closed")
            {
                Image img = new Image();
                img.ImageUrl="~/Images/img.gif";
                item["Status"].Controls.Add(img);
            }
}

Thanks,
Princy
0
Goat_
Top achievements
Rank 1
answered on 12 Apr 2013, 11:24 AM
The code doesn't seem to change the images, it still only has the text in the rows.

Here is an example of 2 Columns i have in my Source

<Columns>                      
<telerik:GridBoundColumn DataField="Status" UniqueName="Status" HeaderText="Status" ItemStyle-Font-Names="Calibri"
      meta:resourcekey="GridBoundColumnResource1"/>
 
            <telerik:GridTemplateColumn UniqueName="ActionColumn" HeaderText="Actions">
 <ItemTemplate>
<asp:HyperLink ID="ActionLink" Text="Action" runat="server"><img src="../Images/icons/action.png" class="images" alt="Delete"></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
 
</Columns>


and here is is my C# code behind


protected void grdIncidents_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                HyperLink actionLink = (HyperLink)e.Item.FindControl("ActionLink");
                actionLink.Attributes["href"] = "#";
                actionLink.Attributes["onclick"] = String.Format("return ShowActionForm('{0}','{1}');",  e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IncidentID"], e.Item.ItemIndex);
 
            }                
        }
 
I tried adding your code into the code behind and editing my source a bit but the images don't load.

The images that are supposed to be loaded into the Status column's rows don't have to be hyperlinks,

Instead of having the following.

Status   
-------
Closed
Open
Closed

I would like those "Open" and "Closed" text values to be replaced with pictures.
0
Accepted
Angel Petrov
Telerik team
answered on 17 Apr 2013, 09:01 AM
Hello,

You can try inserting the image in the PreRender event of the grid. Following the scenario I have created a sample project to test this and it seems to work on my end. Please modify your logic accordingly and let us know of the result.

All the best,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Goat_
Top achievements
Rank 1
answered on 24 Apr 2013, 03:25 PM
Thank you that worked
Tags
Grid
Asked by
Goat_
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Goat_
Top achievements
Rank 1
Angel Petrov
Telerik team
Share this question
or