4

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!

Kira
  • 608
  • 3
  • 10
  • 22

3 Answers3

4

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.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • The RadGrid supports data keys, and has a ton of features for retrieving data. There shouldn't be any need to get a *value* from the cell. – James Johnson Oct 11 '11 at 14:54
  • @James, yes, there should if you want to display the data to the user... Also, you have to use the Value property if the column type is a GridBoundColumn.... There are plenty of scenarios to use the Value property.... – Brian Mains Oct 11 '11 at 15:19
  • I think we're talking about two different things. You can use datakeys rather than getting the text from the cell. – James Johnson Oct 11 '11 at 15:32
  • @James, yes I understand that; however, why use DataKeyNames if you are already displaying the value to the user in the cell... – Brian Mains Oct 11 '11 at 17:32
  • I don't have an issue with your solution. Data keys are just the preferred way to access values in databound list controls. – James Johnson Oct 11 '11 at 17:41
  • Well, both answers are pretty valid, but in my specific case (right now), the "low cost" of changes is to implement a label inside the ItemTemplate... so, thank you all, for now! – Kira Oct 11 '11 at 19:00
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();
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • Isn't there any other way to retrieve the 'literal' content of the cell? I've a specific case, where the following occurs: DataBinder.Eval(Container.DataItem, "InOut").Equals("I") ? "In" : "Out"... so, the DataKey will return a "I" instead "In" (example)... thanks for the reply! – Kira Oct 11 '11 at 16:21
  • You can refer to Brian's answer for grabbing the literal content from the cell. – James Johnson Oct 11 '11 at 16:28
0

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();                
        }
    }
user1040975
  • 420
  • 5
  • 16