我有一個綁定到實體框架對象的GridView。我想訪問GridView的RowDataBound事件中的對象。當我嘗試訪問DataItem對象時,出現一個錯誤,指出無法投射類型爲「System.Data.Objects.MaterializedDataRecord」的對象以鍵入「BBM.NailArtGallery.Model.Item」。顯然,MaterializedDataRecord是一些隱藏的屬性。有誰知道如何解決這個問題。我的GridView綁定使用和EntityDataSource控制。代碼如下。如何在EntitydataSource綁定的GridView中獲取RowDataBound中的實體對象
<asp:EntityDataSource ID="EntityDataSource1" runat="server" AutoPage="true" AutoSort="true" OrderBy="it.ID"
ConnectionString="name=Entities" DefaultContainerName="Entities"
EntitySetName="Items" EntityTypeFilter="ItemImage"
Select="it.[ID], it.[Title], it.[IsFeatured], it.[ImageHome], it.[DateEntered]">
</asp:EntityDataSource>
if (e.Row.RowType == DataControlRowType.DataRow)
{
ICustomTypeDescriptor descriptor = (ICustomTypeDescriptor)e.Row.DataItem;
if (descriptor != null)
{
var prop = (PropertyDescriptor)descriptor.GetProperties()[0];
Item image = (Item)descriptor.GetPropertyOwner(prop);
}
}
工作。謝謝!非常感激! – Paul