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

[Solved] Dynamic Image GridTemplateColunmn problem

4 Answers 145 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anon
Top achievements
Rank 1
Anon asked on 19 Jun 2008, 07:08 PM
Hi,

I'm trying to switch an image in relation to a string within the database for one of the columns in a Grid. So that if if the database field is equal to 'medium' a yellow flag is displayed in the column. I got it to work (the images display in relation to the database), however if there is data in the sub field then it throws and error when I try to expand the row. the exact error reads" ArgumentException was unhandled- Priority is neither a datacolumn nor a datarelation for table default view"

aspx code
OnItemDatabound="Rad2_ItemDataBound" 
 
 
</telerik:GridBoundColumn> 
   <telerik:GridTemplateColumn UniqueName="PriorityProject" Groupable="false"  
           DataField="Priority" HeaderText="Priority"
   <HeaderStyle Width="20px" /> 
   <ItemStyle Height="35px" /> 
   <ItemTemplate> 
   <asp:Image ID="PriorityProject" Boarderwidth="0px" runat="server" ImageUrl='<% Eval "images/icons/" & Trim(objRow("Priority")) & ".png" %>' AlternateText="PriorityFlag" style="float:right;" /> 
    
   </ItemTemplate> 
   </telerik:GridTemplateColumn> 

VB code
 Protected Sub Rad2_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles Rad2.ItemDataBound 
        If e.Item.ItemType = GridItemType.Item Or e.Item.ItemType = GridItemType.AlternatingItem Then 
            Dim objRow As DataRowView 
            objRow = e.Item.DataItem 
            Dim tmpImage As Image 
 
            tmpImage = CType(e.Item.FindControl("PriorityProject"), Image) 
            tmpImage.ImageUrl = "images/icons/" & Trim(objRow("Priority")) & ".png" 
        End If 
 
    End Sub 

thanks in advance, I'm kind of stumped on how to resolve the error as it displays but wont expand

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 Jun 2008, 05:20 AM
Hi Anon,

Try setting the Name property for the MasterTable and try accessing the image as shown below.

CS:
 
protected void Rad2_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
 { 
  if (e.Item.OwnerTableView.Name == "Master") 
   {  
 
     if (e.Item.ItemType == GridItemType.Item | e.Item.ItemType == GridItemType.AlternatingItem) 
      { 
         DataRowView objRow; 
         objRow = e.Item.DataItem; 
         Image tmpImage; 
         
         tmpImage = (Image)e.Item.FindControl("PriorityProject"); 
         tmpImage.ImageUrl = "images/icons/" + Strings.Trim(objRow("Priority")) + ".png"; 
     } 
   } 
     
 } 
  


Thanks
Shinu.

0
Anon
Top achievements
Rank 1
answered on 20 Jun 2008, 01:37 PM
hellow Shinu,

I cant believe I didnt think about giving the overviewtable a name. Thank you for your advice, however there is one catch, now with the decleration of e.Item.Owner.TableView.Name="Master"  the columns now return with the default imageUrl from the aspx page. The rows expand but do not match the data from the field returning from the database. Wondering if you had a suggestion. Thanks agian

 
 
OnItemDatabound="Rad2_ItemDataBound"       
       
       
</telerik:GridBoundColumn>       
   <telerik:GridTemplateColumn UniqueName="PriorityProject" Groupable="false"        
           DataField="Priority" HeaderText="Priority">       
   <HeaderStyle Width="20px" />       
   <ItemStyle Height="35px" />       
   <ItemTemplate>       
   <asp:Image ID="PriorityProject" Boarderwidth="0px" runat="server" ImageUrl='images/icons/medium.png' AlternateText="PriorityFlag" style="float:right;" />       
          
   </ItemTemplate>       
   </telerik:GridTemplateColumn   
 
 
Protected Sub Rad2_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.GridItemEventArgs) Handles Rad2.ItemDataBound       
    
If e.Item.OwnerTableView.Name="Master" then     
        If e.Item.ItemType = GridItemType.Item Or e.Item.ItemType = GridItemType.AlternatingItem Then       
            Dim objRow As DataRowView       
            objRow = e.Item.DataItem       
            Dim tmpImage As Image       
       
            tmpImage = CType(e.Item.FindControl("PriorityProject"), Image)       
            tmpImage.ImageUrl = "images/icons/" & Trim(objRow("Priority")) & ".png"       
        End If       
  End If     
    End Sub      
 
0
Anon
Top achievements
Rank 1
answered on 22 Jun 2008, 03:58 PM
Hi all,

Shinu had it right, I was just kind of dumb and forgot to add the name property to the aspx side of the grid. So if you want to use a text value and change with an image in the column then by all means use this, works great. I used it for a Task/Project management project for setting priority to a task or project. Just dont forget to name your MasterView to the same name as your VC/C# code as well.     

<MasterTableView NoMasterRecordsText="Object Not found" DataKeyNames="TaskID"  
                datasourceid="sqlds" Name="Master"

If e.Item.OwnerTableView.Name="Master"  

Thanks go out to Shinu and the telerik team
0
Shinu
Top achievements
Rank 2
answered on 23 Jun 2008, 04:25 AM
Hi Anon,

You can also have a look at the following help article.
Distinguish grid rows in hierarchy on ItemCreated/ItemDataBound

Thanks
Shinu.

Tags
Grid
Asked by
Anon
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Anon
Top achievements
Rank 1
Share this question
or