GridDataItem

4 posts, 0 answers
  1. mww
    mww avatar
    257 posts
    Member since:
    Sep 2012

    Posted 08 Jun 2010 Link to this post

    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
  2. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 08 Jun 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Arnie Vargas
    Arnie Vargas avatar
    7 posts
    Member since:
    Oct 2012

    Posted 29 Sep 2011 Link to this post

    Is it possible to get the VB version of the solution?
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 Sep 2011 Link to this post

    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.
Back to Top