C# - 在GridView中为多个列使用相同的对象

时间:2011-05-05 05:12:27

标签: c# asp.net data-binding gridview


我有一个学生注册网格视图填充了注册对象。 Enrollment对象具有学生ID。使用该Id,我将填充注册gridview的2列(来自Student对象的3个属性) 现在,我正在使用下面的代码。这意味着同一个对象的数据库往返3次,这非常糟糕。如何使用相同的对象来获取3个属性?

<asp:TemplateField HeaderText="Student Name">
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server" Text='<%# string.Format("{0} - {1}", StudentProfile.GetStudentProfileById(Convert.ToInt32(Eval("StudentId"))).FirstName, StudentProfile.GetStudentProfileById(Convert.ToInt32(Eval("StudentId"))).Surname) %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Student DOB">
                <ItemTemplate>
                    <asp:Label ID="lblDOB" runat="server" Text='<%# StudentProfile.GetStudentProfileById(Convert.ToInt32(Eval("StudentId"))).DateOfBirth %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

编辑:仅供参考,我必须处理更新gridview行,这将涉及向每行添加文本框等。你能不能回答这个问题吗?

非常感谢!

1 个答案:

答案 0 :(得分:2)

将数据绑定到gridview的方式是错误的。您需要获取对象/集合中的Data,然后将该数据作为DataSource绑定到GridView

在这里查看Displaying Data With the ObjectDataSource并查看此Querying Data with the SqlDataSource Control

相关问题