I would like to read the value from the e.Item.DataItem into a string but for whatever reason, I can't seem to get it, although I can see it in the watch window
Can you add some more information? Do you literally want a string value or do you need a particular string from the value?
If you need a string value that should be doable. The watch window tends to display values by calling .ToString on the object. If the value in the watch window is the string you want then you should be able to get it by simply calling ToString.
var item = e.Item.DataItem.ToString();
You'd need to do a null check on the various properties as well.
It sounds like you've done something like:
obj.Datasource = (from .... select new { a=..., b=...}).ToList();
And for one of your items, you want to get the values for 'a' or 'b' out, right?
You can, but it's complicated (and not recommended). To do this, you'd have to cast the e.Item.DataItem to the correct class, but that's an anonymous type, which means you can't specify the cast without some trickery. You could try something like this:
private T ForceCast(T prototype, object obj)
{
return (T)obj;
}
Then, call ForceCast(new {a=...,b=...}, e.Item.DataItem). I think that'll work if you're in the same assembly (and the values you supply for a and b get the type right -- the compiler will guess T for you and figure things out). However, the better way is to just define a real class to hold the data you're returning from your datasource. Then you can cast e.Item.DataItem to that class and you'll be fine.
(note: I haven't actually tried to run this code -- this was just a thought I had while reading this, but I'm thinking I've seen it before. Maybe it was this blog?)
Got the following from a similar question
DataBinder.Eval(e.Item.DataItem, "PropertyName")