4
votes

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!

3

3 Answers

4
votes

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.

2
votes

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();
0
votes

Here's another way, as I used the DataBinder to get my literal content as well as display it:

 protected void RadGrid_OnItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
            var test = DataBinder.Eval(dataItem.DataItem, "Column").ToString();                
        }
    }