Retrieve value from GridTemplateColumn in server side (Telerik RadGrid)

16,994

Solution 1

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.

Solution 2

You should be using datakeys to retrieve values from the grid.

You can use the DataKeyNames property on the MasterTableView to specify the columns you need, like this:

<telerik:RadGrid ID="RadGrid1" runat="server" ...>
    <MasterTableView DataKeyNames="Col1, Col2, Col3" ...>

And then in the code-behind:

string col1 = RadGrid1.Items[0].GetDataKeyValue("Col1").ToString();
Share:
16,994
Kira
Author by

Kira

.NET Developer

Updated on June 07, 2022

Comments

  • Kira
    Kira almost 2 years

    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!