How do you bind a gridview column to a subclass value?

前端 未结 4 448
失恋的感觉
失恋的感觉 2020-12-20 19:57

I have a ASP.net gridview that I am trying bind to. My DataSource has a collection and 2 of the columns I am binding to are part of a subclass. My DataSource has a subclas

相关标签:
4条回答
  • 2020-12-20 20:36

    The data binding mechanism behind ASP.NET GridView supports only one level bindings. (as opposed to its WinForms Binding counterpart that supports multi-level in the case of binding to a DataSet / DataTable / DataView).

    You have three possible solutions:

    1. Handling the ItemDataBound event for each row
    2. Extending your root level entities with properties that expose the child object properties and using these properties for the binding expressions
    3. Instead of using a BoundField you could use a Template Field and generate the content using a <%= %> expression that accesses the Data Item.
    0 讨论(0)
  • 2020-12-20 20:38

    The [Name].[Name] syntax is not supported by BoundField. Only simple property names.

    0 讨论(0)
  • 2020-12-20 20:41

    Mark,

    I am 99.9% sure that you will have to handle this in the codebehind on the ItemDataBound event for the individual row.

    Remember you can get the whole databould object from e.Item.DataItem

    0 讨论(0)
  • 2020-12-20 20:45

    I believe you can get this to work using a Template field and a markup scriptlet...

        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label Id="lblSubclassVal" runat="server" Text="<%# DataBinder.Eval(Container.DataItem, "SubClass.PropertyName")%>"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    
    0 讨论(0)
提交回复
热议问题