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

GridDataItem

3 Answers 517 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mww
Top achievements
Rank 1
mww asked on 08 Jun 2010, 12:13 PM
I have a grid that uses a GridTemplateColumn.  In the ItemDataBoundEvent  I want to get access to the underlying column data, heres the markup

<telerik:GridTemplateColumn DataField="SportingEvent" UniqueName="IsSportingEvent" HeaderText="Sporting Event ?"
                                               <ItemTemplate> 
                                               <asp:Label ID="LabelSporting" runat="server" Text='<%#Eval("SportingEvent") %>'></asp:Label> 
                                            </ItemTemplate> 
                                        </telerik:GridTemplateColumn> 
  and the codebehind

protected void RadGridCategories_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        { 
            if (e.Item is GridDataItem) 
            { 
                GridDataItem item = e.Item as GridDataItem; 
                 
                string se = item["IsSportingEvent"].Text; 
                               
                Label lbl = item["IsSportingEvent"].FindControl("LabelSporting") as Label; 
                if (lbl.Text == "False") 
                { 
                    lbl.Text = "No"
                } 
                if (lbl.Text == "True") 
                { 
                    lbl.Text = "Yes"
                } 
                 
                 
 
            } 
        } 
  Ive tried to use the item index method, but its always empty, in the end I had to resort to getting the value of the label.  How do I gain access to the underlying data from the columns ?

surely string se = item["IsSportingEvent"].Text;  should have worked

3 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 08 Jun 2010, 12:38 PM
Hi mww,

Try the following code snippet to access the Label from the GridTemplateColumn.
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            GridDataItem item = (GridDataItem)e.Item;  
            Label lbl = (Label)item["IsSportingEvent"].FindControl("LabelSporting");  
        }  
           
    }

Additionally, I suggest that you review the help article below:
Accessing cells and rows

Kind regards,
Pavlina
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Arnie Vargas
Top achievements
Rank 1
answered on 29 Sep 2011, 07:46 PM
Is it possible to get the VB version of the solution?
0
Princy
Top achievements
Rank 2
answered on 30 Sep 2011, 04:59 AM
Hello Arnie,

Try the following VB code snippet access the Label from the GridTemplateColumn.
VB:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
{
  If TypeOf e.Item Is GridDataItem Then
    Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
    Dim lbl As Label = DirectCast(item("IsSportingEvent").FindControl("LabelSporting"), Label)
  End If
}

Thanks,
Princy.
Tags
Grid
Asked by
mww
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Arnie Vargas
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or