This is a migrated thread and some comments may be shown as answers.
ItemDataBound on GridTemplateColumn
8 Answers 695 Views
This is a migrated thread and some comments may be shown as answers.
Levi
Top achievements
Rank 1
Levi asked on 30 Sep 2008, 03:27 AM
I'm trying to capture the ItemDataBound event to conditionally alter the contents of a GridTemplateColumn. The problem is the code example I found assumes you are altering a standard GridBoundColumn. I actually have two databound fields in my GridTemplateColumn and I don't see a way to update their values. 

Here is my code:
        protected void RadGridSummary_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.AlternatingItem | e.Item.ItemType == GridItemType.Item)
            {
               
                GridDataItem gridItem = (GridDataItem)e.Item;
                if (String.IsNullOrEmpty(gridItem["ReferrerName"].Text) == true)           
                {
                    gridItem["ReferrerName"].Text = "(None)";
                
            }
        }

I can only reference the name of the GridTemplateField "Referrer" and I don't know how to access the 2 data bound columns using the object model. Here is my ASPX code:

<telerik:GridTemplateColumn UniqueName="Referrer"                     SortExpression="ReferrerName" HeaderText="Referrer / Keyword">
                    <HeaderStyle Width="150px"></HeaderStyle>
                    <ItemStyle Height="35px"></ItemStyle>
                    <ItemTemplate>
                        <span class="gridItemText">
                            <%# DataBinder.Eval(Container.DataItem, "ReferrerName") %>
                        </span>
                        <br />
                        <span class="gridItemTextAlt">
                            <%# DataBinder.Eval(Container.DataItem, "Keyword") %>
                        </span>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>

I want be able to capture the event of the ReffererName field being handled so I can alter its value. I saw another example that showed updating a checkbox in another field by using it's control collection, but I did not see how this could be applied to the GridTemplateColumn. Here is the original article I found which shows how to update a regular field:
http://www.telerik.com/help/aspnet/grid/radgrid-telerik.webcontrols.radgrid-itemdatabound_ev.html


Any help would be greatly appreciated!

Thanks,
Levi

8 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 02 Oct 2008, 11:16 AM
Hi Levi,

To see how to reference the item and the cells properly, please refer to the following article. Let me know if it gets you started properly.

Greetings,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Tony
Top achievements
Rank 2
answered on 30 Jan 2009, 07:37 PM

I would like to know how to do this too.

Yavor, the article you referenced was not helpful.  In fact, the article describes how to do what Levi already has shown in his code.  The issue here is how do you get the value of a databound field inside a gridtemplatecolumn.

I was able to do a workaround which was to put the databound field inside a Label control and inside my code block I did a FindControl and conditionally evaluated the text field of the control and reset the text field if it was necessary:

Pascal Code:

 
   if (e.Item is Telerik.Web.UI.GridDataItem) then 
   begin 
 
      lblOldDate := e.Item.FindControl('lblOldDate'as System.Web.UI.WebControls.Label;  
 
      if lblOldDate.Text = '' then 
         lblOldDate.Text := '' 
      else 
         lblOldDate.Text := 'Old Date: ' + lblOldDate.Text;  
   end

Thanks,
Tony

0
Yavor
Telerik team
answered on 02 Feb 2009, 03:15 PM
Hello Tony,

The key point here is that after you have referenced a given cell, you can use the .Controls collection (or the FindControl function, passing the id of the control you want to refer), to get a reference to the control nested in the template item cell. This is the case, because the template can host many controls, unlike the standard grid bound column, where to get the value, you can use the .Text property.
So, for example, if you have a template like:

.aspx
   <telerik:GridTemplateColumn UniqueName="template1">  
                <ItemTemplate> 
                <asp:Label runat="server" ID="Label1">  
                </asp:Label> 
                </ItemTemplate> 
                </telerik:GridTemplateColumn> 

you can dynamically alter the text as shown in the code below:

.cs
   protected void Grid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            GridDataItem dataItem = (GridDataItem)e.Item;  
            (dataItem["template1"].FindControl("Label1") as Label).Text="Custom text";  
        }  
    } 

I hope this information helps.

Regards,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Tony
Top achievements
Rank 2
answered on 02 Feb 2009, 04:48 PM
Yavor:

Unfortunately, it does not help me.  If you read my post, what you described is exactly what I'm already doing.  I understand that part.

The problem is that I don't want to have to use controls to display the data.  I don't want to have to use a Label; I simply want to use the Eval function to display the data and I want to be able to reference the field from my ItemDataBound procedure without having to FindControl.

Is there anyway to do this?  If not, I guess the only other option I have is to iterate through the DataSet programatically, modify any cells that need modification, then assign the DataSet to the RadGrid control.  Does that sound right?

Tony
0
Yavor
Telerik team
answered on 03 Feb 2009, 12:17 PM
Hi Tony,

In this case, you can abandon using the label approach.
For example, let us assume that you have a template column defined as:

.aspx
<telerik:GridTemplateColumn UniqueName="template1">     
<ItemTemplate>    
<%#Eval("Address") %>                
</ItemTemplate>    
</telerik:GridTemplateColumn>   

From the code behind, you can alter the text in the cells like this:

.cs
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            GridDataItem dataItem = (GridDataItem)e.Item;  
            dataItem["template1"].Text = "Custom text";  
        }     
 
    } 

since there are no controls nested in the cell, you can alter its text directly.
Also attached is a small project sample.
Let me know if I am leaving something out.

Sincerely yours,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Tony
Top achievements
Rank 2
answered on 04 Feb 2009, 12:42 AM
Yavor:

Almost, but not quite.  What happens when you have multiple databound fields you are referencing inside the template?  After all, that's part of the reason for using a GridTemplateColumn....so you can arrange a custom template of multiple fields.

So in the following example, how do you alter just the value of the "Phone" field?  Let's say the Phone field is empty and you want to display 'N/A' instead.

<telerik:GridTemplateColumn UniqueName="template1">        
<ItemTemplate>       
<%#Eval("Address") %> 
<%#Eval("Phone") %>                    
</ItemTemplate>       
</telerik:GridTemplateColumn>    
 

It seems like there should be a call like:

dataItem["template1"].item['Phone'].Text = "Custom text"

Or something like that.

Tony
0
Yavor
Telerik team
answered on 09 Feb 2009, 09:05 AM
Hi Tony,

In this case, the approach is difference.
Since you have no nested controls (no labels for each entry), you can use code like the following to get the text:

.cs
(dataItem["template1"].Controls[0] as DataBoundLiteralControl).Text 

You would need to parse this, and determine what changes need to be made. The reason why the text is rendered like this is that there is no single cell (as is the case with only one eval statement, nor are there multiple controls, like labels, which can be accessed, to change their value.
I hope this information helps.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mahdy
Top achievements
Rank 1
answered on 28 Mar 2013, 06:39 AM
Hi

I think you can use MasterTableView's DataKeyNames property. First, you should set this property (liks this DataKeyNames = "Address,Phone") and then you can use it on ItemDataBound event.

for example,
    protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            string Address = dataItem.GetDataKeyValue("Address").ToString();
            string Phone = dataItem.GetDataKeyValue("Phone").ToString();
        }
    }

Tags
Grid
Asked by
Levi
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Tony
Top achievements
Rank 2
Mahdy
Top achievements
Rank 1
Share this question
or