2009-07-15 14 views
33

私は以下にリストされているようなオブジェクトマップを持っています。私は、GridViewコントロールにNestedClassのプロパティをバインドしようとすると、私はエラーを取得する:GridViewはネストされたクラスのプロパティでバインドされました

"A field or property with the name 'NestedClass.Name' was not found on the selected data source."

GridViewのは、ObjectDataSourceのにバインドされ、ObjectDataSourceのはBoundClassのフル装備のインスタンスにバインドされています。

これを回避する手段はありますか?

サンプルクラス:インスタンスの

public class BoundClass 
{ 
    public string Name { get; set; } 
    public NestedClass NestedClass { get; set; } 
} 

public class NestedClass 
{ 
    public string Name { get; set; } 
} 
+0

列名「NestedClass.Name」は、取得元のデータからは存在しません。 – waqasahmed

答えて

52

のみ即時性がBoundField列に表示することができます。

代わりに、バインドフィールドに割り当てるのではなく、ネストされたプロパティにアクセスするには、itemtemplateでDataBinder.Evalを使用する必要があります。

例:また

<asp:TemplateField> 
    <itemtemplate> 
     <p><%#DataBinder.Eval(Container.DataItem, "NestedClass.Name")%></p> 
    </itemtemplate> 
</asp:TemplateField> 

、あなたはBoundFieldを継承し、このブログの記事で説明したように、DataBinder.Evalを使用するようにGetValueメソッドをオーバーライドするカスタムクラスを作成することができます。

http://web.archive.org/web/20120121123301/http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx

+8

壊れたリンクのためのウェイバックマシン:http://web.archive.org/web/20120121123301/http://iridescence.no/post/FixingBoundFieldSupportforCompositeObjects.aspx – user423430

10

このBoundFieldの拡張機能では、ネストされたプロパティをサポートするが呼び出されます。

public class BetterBoundField : BoundField 
{ 
    protected override object GetValue(Control controlContainer) 
    { 
     if (DataField.Contains(".")) 
     { 
      var component = DataBinder.GetDataItem(controlContainer); 
      return DataBinder.Eval(component, DataField); 
     } 
     return base.GetValue(controlContainer); 
    } 
} 
+0

+1の拡張メソッド – mcfea

関連する問題