GridView에 데이터 원본으로 사람 배열을 설정하면 두 개의 속성 인 성 및 이름이있는 Person 클래스가 있습니다 어떻게 이름을 모두 표시 할 수 있습니까? 한 열에서?/성GRid보기 asp.net의 한 열에 두 속성을 표시하는 방법 C#
고맙습니다 당신은 코드 숨김 내가 그 일을 생각하지 않는 경우, 여기 방법
GridView에 데이터 원본으로 사람 배열을 설정하면 두 개의 속성 인 성 및 이름이있는 Person 클래스가 있습니다 어떻게 이름을 모두 표시 할 수 있습니까? 한 열에서?/성GRid보기 asp.net의 한 열에 두 속성을 표시하는 방법 C#
고맙습니다 당신은 코드 숨김 내가 그 일을 생각하지 않는 경우, 여기 방법
.
는 ItemTemplate
에 라벨을 containg의의 GridView에 TemplateField
을 넣고 선까지 RowDataBound의 핸들러에의 GridView의 이벤트 : 핸들러에서
<asp:GridView runat="server" ID="PeopleGridView" AutoGenerateColumns="false" OnRowDataBound="PeopleGridView_OnDataBound" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="NameLabel" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
는있는 DataItem 얻고로 캐스팅하여 Person 클래스. Label 컨트롤을 찾아 클래스에서 속성에 텍스트를 설정합니다
protected void PeopleGridView_OnDataBound(object sender, GridViewRowEventArgs e)
{
// Make sure it's a DataRow - this will fail for HeaderRow, FooterRow etc
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the DataItem and cast it
Person currentPerson = (Person) e.Row.DataItem;
// Locate the Label and set it's text
((Label) e.Row.FindControl("NameLabel")).Text = currentPerson.firstName + " " + currentPerson.lastName;
}
}
사용 tempate 필드와 평가 방법 :
<asp:GridView runat="server" ID="MyGrid" AutoGenerateColumns="false"
DataSourceId="...">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("FirstName") %> <%# Eval("LastName") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>