Retrieve value from GridTemplateColumn in server side (Telerik RadGrid)

Kira picture Kira · Oct 11, 2011 · Viewed 16.8k times · Source

I have the following code in my aspx page (simplified):

<telerik:RadGrid ID="rgd_grid" runat="server">
<MasterTableView>
<Columns> 
     <telerik:GridTemplateColumn UniqueName="Unique" HeaderText="Header" DataField="dataField">
     <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem, "expression") %>
     </ItemTemplate>
     </telerik:GridTemplateColumn> 
</Columns>
</MasterTableView>

I just need to do a loop in the grid to retrieve the values of the cells in the code-behind, but I've find no way to get the value in the "Eval" expression... I've try the following:

rgd_grid.MasterTableView.Items[0]["Unique"].Text;

But the text property is empty, while all the others are correct. Actually, I've tried a lot of other things, but this seems to be the most close to the objective.

Regards, I appreciate every help!

Answer

Brian Mains picture Brian Mains · Oct 11, 2011

Are you sure the item returned is not the header or something like that? I think the header is included in the results, but could be wrong. Add a check like:

var item = rgd_grid.MasterTableView.Items[0] as GridDataItem;
if (item != null)
    string text = item["Unique"].Text;

If that doesn't work, you can always resort to using a Label control within the template, and finding the control by ID.