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

Programmatic image for GridButtonColumn

2 Answers 350 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jaytee
Top achievements
Rank 1
Jaytee asked on 18 Nov 2009, 04:51 PM
Hi all

I'm after some help with achieving the following.

I am programmatically creating my RadGrid and it is to contain GridBoundColumns and a GridButtonColumn. The grid button column is to allow for clicking and opening another page. However I would like the ImageUrl for the grid button column to be dependant upon a column value in the datatable. For instance if the value in the column of the row in the datatable is true then show one image but if it is false then show another.

At this point my ascx page looks like so:

<

 

telerik:RadGrid ID="radGrid1" runat="server" OnItemDataBound="DataBoundMethod">

 

 

 

<MasterTableView>

 

 

 

<RowIndicatorColumn>

 

 

 

<HeaderStyle Width="20px"></HeaderStyle>

 

 

 

</RowIndicatorColumn>

 

 

 

<ExpandCollapseColumn>

 

 

 

<HeaderStyle Width="20px"></HeaderStyle>

 

 

 

</ExpandCollapseColumn>

 

 

 

</MasterTableView>

 

 

 

<ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">

 

 

 

</ClientSettings>

 

</

 

 

telerik:RadGrid>

 

 


My ascx.cs page then includes (amongst other btis of code) the following method where I thought I could do what I wanted

protected

 

void DataBoundMethod(object sender, Telerik.Web.UI.GridItemEventArgs e)

 

{

 

 

    if (e.Item is GridDataItem)

 

    {

 

 

        GridDataItem dataItem = e.Item as GridDataItem;

 

 

 

        
        //Need to do something here to alter the ImageUrl
     }

 

}

Any thoughts on this? If I am going down the wrong road that is ok, I'm fairly new to the Telerik stuff so am still learning. Any help be would be greatly appreciated.

Many thanks

 

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Nov 2009, 04:38 AM
Hi Jaytee,

Here is the code that I tried to set the imageurl from code behind.

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

CS:
 
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/AddRecord.gif"
            } 
            else 
            { 
                imgButton.ImageUrl = "../Images/Refresh.gif"
            } 
        }              
    } 

-Shinu.
0
Jaytee
Top achievements
Rank 1
answered on 19 Nov 2009, 03:08 PM
Hi

Thanks for the feedback. I have opted for a solution wherby I use CheckBoxColumns for my boolean result columns from the dataset and then set their visibility to false. Then in the OnItemDataBound event I set the ImageUrl of my GridButtonColumns based on the Checked value of the CheckBoxColumn.

Hope that made sense.

Again thanks for the response.
Tags
Grid
Asked by
Jaytee
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jaytee
Top achievements
Rank 1
Share this question
or