Accessing a value in a radGrid column

Csharp picture Csharp · May 11, 2013 · Viewed 7.2k times · Source

I want to access a value in a radGrid control. Given the image below, I want to access the value of "Status". But I can't seem to get it.

I get an error message

"Unable to cast object of type 'TripLeg' to type 'Telerik.Web.UI.GridDataItem'."

Any ideas how to access that column?

GridItem

Answer

Win picture Win · May 11, 2013

You are almost there. You just need to cast DataItem to appropriate object. Let assume your data source is IEnumerable<TripLeg>.

Here is the example -

if (e.Item is GridDataItem)
{
   var item = e.Item as GridDataItem;
   var tripLeg = e.Item.DataItem as TripLeg; // Cast to appropriate object
   var status = tripLeg.Status; 

   // var hLink = (HyperLink) item.FindControl("HyperLink1");
   // Above code will throw exception if the control is not found.

   var hLink = item.FindControl("XXXXX") as HyperLink;
   if(hLink != null)
   {
      hLink.Attributes.Add("XXXXX", "XXXXX");
   }
}