Get value from server-side GridTemplateColumn (Telerik RadGrid)

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 get the values ​​of the cells in the code, but I did not find a way to get the value in the expression "Eval" ... I tried the following:

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

But the text property is empty, and all the others are true. In fact, I tried many other things, but this seems to be the closest to the goal.

Sincerely, I appreciate every help!

+4
source share
3 answers

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

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

If this does not work, you can always resort to using the Label control in the template and find the control by identifier.

+4
source

You should use datakeys to extract values ​​from the grid.

You can use the DataKeyNames property in the MasterTableView to specify the desired columns, for example:

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

And then in the code:

 string col1 = RadGrid1.Items[0].GetDataKeyValue("Col1").ToString(); 
+2
source

Here's another way, since I used a DataBinder to get my literal and 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(); } } 
0
source

All Articles