Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a value in a radGrid column

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

like image 362
Csharp Avatar asked May 10 '13 22:05

Csharp


2 Answers

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");
   }
}
like image 121
Win Avatar answered Oct 14 '22 14:10

Win


I like Telerik Components a lot (altough more and more I like Kendo UI) anyway seems to me that if you want to get the value on the status you could use this

string itemValue = dataItem["ColumnUniqueName"].Text;
//no need to convert :)

Take a look at the Documentation for the RadGrids... http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html

like image 27
Marcianin Avatar answered Oct 14 '22 16:10

Marcianin