将COllection绑定到Gridview

时间:2013-03-05 10:32:15

标签: asp.net gridview collections

List<Person> pList = new List<Person>();
            pList.Add(new Person(1, "John", "", "Shields", 29, 'M'));
            pList.Add(new Person(2, "Mary", "Matthew", "Jacobs", 35, 'F'));
            pList.Add(new Person(3, "Amber", "Carl", "Agar", 25, 'M'));
            pList.Add(new Person(4, "Kathy", "", "Berry", 21, 'F'));
            pList.Add(new Person(5, "Lena", "Ashco", "Bilton", 33, 'F'));
            pList.Add(new Person(6, "Susanne", "", "Buck", 45, 'F'));
            pList.Add(new Person(7, "Jim", "", "Brown", 38, 'M'));
            pList.Add(new Person(8, "Jane", "G", "Hooks", 32, 'F'));
            pList.Add(new Person(9, "Robert", "", "", 31, 'M'));
            pList.Add(new Person(10, "Cindy", "Preston", "Fox", 25, 'F'));
            pList.Add(new Person(11, "Gina", "`enter code here`", "Austin", 27, 'F'));
            pList.Add(new Person(12, "Joel", "David", "Benson", 33, 'M'));
            pList.Add(new Person(13, "George", "R", "Douglas", 55, 'M'));
            pList.Add(new Person(14, "Richard", "", "Banks", 22, 'M'));
            pList.Add(new Person(15, "Mary", "C", "Shaw", 39, 'F'));
            gv1.DataSource = pList;
            gv1.DataBind();

我想在gridview中显示所选字段。如何使用EVAL功能?另外如何使用DataField属性?

1 个答案:

答案 0 :(得分:1)

您好,您可以使用ItemTemplate

<asp:GridView ID="GridView1" runat="server">
   <Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <%# Eval("Name")%>
            <br/>
            <%# Eval("Age")%>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>

或查看以下链接:

http://www.dotnetspider.com/resources/29877-Binding-Gridview-generic-list.aspx