Entity Framework GridView RowDataBound

I found it tricky to get actual entities in the GridView RowDataBound event.

Per Diego Vega’s post here, this may only happen because I am using the EntitySetName property of the EntityDataSource.

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
            Dim descriptor As ComponentModel.ICustomTypeDescriptor = e.Row.DataItem

If descriptor IsNot Nothing Then
                Dim prop = descriptor.GetProperties().Cast(Of ComponentModel.PropertyDescriptor)().First()

Dim Order As Order = DirectCast(descriptor.GetPropertyOwner(prop), Order)

Response.Write(“Order Number:” & Order.OrderNumber)
End If
End If
End Sub