0


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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

You mean I should create a class to store intermediate objects that reflects by GridView columns?
You can get all the data at once from Database and then put it some collection/datatable and then bind that collection/datatable to your grdivew

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.