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

Setting ImageURL in GridButtonColumn

2 Answers 390 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rich
Top achievements
Rank 1
Rich asked on 28 Jun 2010, 09:30 PM
I have a GridButtonColumn defined in within a Grid control which needs to display diffeent images depending on the data source row. 

However, there doesn't seem to be a way of setting the ImageURL property of this control from a data source column value.  How can this be done?

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 29 Jun 2010, 06:45 AM
Hello Richard,

You can change the ImageUrl of the Buttons in ItemDataBound event after checking  for the condition. Here is an example.

Markup:
 
  
 <MasterTableView AutoGenerateColumns="False" EditMode="InPlace" CommandItemDisplay="Top"  
                DataSourceID="SqlDataSource1" DataKeyNames="CustomerID">  
           <Columns>  
                <telerik:GridButtonColumn ButtonType="ImageButton" UniqueName="ButtonColumn">  
                </telerik:GridButtonColumn>  
                . . .  

Code behind:
  
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            GridDataItem item = (GridDataItem)e.Item;  
            ImageButton imgButton = (ImageButton)item["ButtonColumn"].Controls[0];  
            if (item.GetDataKeyValue("CustomerID").ToString() == "ANATR"// I set the DataKeyNames as CustomerID  
            {  
                imgButton.ImageUrl = "../Images/Img1.gif";  
            }  
            else  
            {  
                imgButton.ImageUrl = "../Images/Img2.gif";  
            }  
        }               
    }  


-Shinu.
0
Rich
Top achievements
Rank 1
answered on 29 Jun 2010, 10:32 PM
Thank you - this should work
Tags
Grid
Asked by
Rich
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Rich
Top achievements
Rank 1
Share this question
or