开发者

C# - Use same object for multiple columns in GridView

I have a student enrollment gridview populated with Enrollment objects. The Enrollment object has student Id. With that Id, I am populating 2 columns (from 3 properties of Student object) of enrollment gridview.

Right now, I am doing it with below code. This means 3 roun开发者_开发百科d trips to database for the same object, which is so very bad. How can I use the same object to get the 3 properties?

<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>

Edit: FYI, I have to deal with updating the gridview rows which will involve adding textboxes and so to each row. Can you please keep the answers apt to this point?

Thank you very much!


The way you are binding data to the gridview is wrong. You need to get the Data in an Object/Collection and then bind that data as a DataSource to your GridView.

Take a look here Displaying Data With the ObjectDataSource and look at this as well Querying Data with the SqlDataSource Control

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜