Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

asp.net - How do you bind a gridview column to a subclass value?

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 subclass called Staff the contains the staff information. The boundfields SurveyID and NumberOfExceptions bind fine, but the Staff.Name and Staff.Office cannot be bound.

asp:BoundField DataField="SurveyID" HeaderText="ID" ...
asp:BoundField DataField="Staff.Name" HeaderText="Name" ...
asp:BoundField DataField="Staff.Office" HeaderText="Office" ...
asp:BoundField DataField="NumberOfExceptions" HeaderText="Exceptions" ...

And the code behind is:

uxSurveyGrid.DataSource = searchResults;
uxSurveyGrid.DataBind();

If I type searchResults[0].Staff.Name in the code behind I can see the value, why is the runtime not being able to evaluate Staff.Name in the gridview?

How do you bind the columns to the subclass values? Do I have to do it in codebehind?

Any help would be appreciated,

Mark.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...