Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6733763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:48:30+00:00 2026-05-26T10:48:30+00:00

Wondering the best way to populate a GridView with my object data. I have

  • 0

Wondering the best way to populate a GridView with my object data.

I have to show a product list from an complex object Sale, whose structure is something like this:

class Sale {
    int id;
    List<SaleItem> saleItems;
}

class SaleItem {
    int id;
    int quantity;
    Product product;

    BillingAddress billingAddress;
    ShippingAddress shippingAddress;
}

class Product {
    int id;
    string name;
    List<BuyingConfiguration> buyingConfigurations;
}

class BuyingConfiguration {
    string name; // like size, color, material
    string value;
}

and my grid should look like this:

Sale Items
+---------+---+------------+------------+----------------+
| Name    | # | B. Address | S. Address | Configurations |
+---------+---+------------+------------+----------------+
| Ferrari | 2 | --         | --         | Color: red     |
|         |   |            |            | Engine: Xyz    |
+---------+---+------------+------------+----------------+
| Jax     | 1 | --         | --         | Color: blue    |
|         |   |            |            | Engine: Abc    |
+---------+---+------------+------------+----------------+

Should I implement an ObjectDataSource for my Sale object? Is there any better solution?


EDIT 2: Let me try to make myself clear: the problem is not how to the display configurations.
My problem is that the Sale object is returned to my code from the persistence layer, thats the why I dont want the GridView to access Database directly. Instead, it needs to load all its data from my Sale object, how to achieve that?


EDIT:

The Grid markup as requested:

<asp:GridView runat="server" ID="GridProdutos" OnRowDataBound="GridProdutos_OnRowDataBound"
    AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField HeaderText="Name" />
        <asp:BoundField HeaderText="#" />
        <asp:BoundField HeaderText="B. Address" />
        <asp:BoundField HeaderText="S. Address" />
        <asp:BoundField HeaderText="Configurations" />
    </Columns>
</asp:GridView>

Ugly solution so far, using OnRowDataBound (I want to avoid that!):

protected void GridProdutos_OnRowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.DataItem == null)
        return;

    SaleItem item = (SaleItem )e.Row.DataItem;

    e.Row.Cells[0].Text = item.product.name;
    e.Row.Cells[1].Text = item.quantity.ToString();

    StringBuilder sbConfigurations = new StringBuilder();

    foreach (BuyingConfiguration configurationItem in item.product.buyingConfigurations) {
        sbConfigurations.AppendFormat("{0}: {1}<br />", configurationItem.name, configurationItem.value);
    }
    e.Row.Cells[4].Text = sbConfigurations .ToString();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:48:31+00:00Added an answer on May 26, 2026 at 10:48 am

    I would suggest using the TemplateColumns with binding expressions. You can bind your GridView to the saleItems list, and implement getter methods to render each field given a SaleItem instance. For example, your Name column could be defined as follows:

                <asp:TemplateField>
                    <ItemTemplate>
                        <%# ((SaleItem)Container.DataItem).product.Name %>
                    </ItemTemplate>
                </asp:TemplateField>
    

    The same thing could be done with a custom getter method to move the access details into the code-behind:

                <asp:TemplateField>
                    <ItemTemplate>
                        <%# getSaleItemProductName((SaleItem)Container.DataItem) %>
                    </ItemTemplate>
                </asp:TemplateField>
    

    Don’t forget to add an Import directive to be able to reference your types:

    <%@ Import Namespace="YouNamespaceHere" %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering what the best way is using php to obtain a list
I was wondering about the best way to manage projects dependencies from ant. What
I was wondering what the best way to model a relationship where an object
I'm wondering the best way to approach this. I have a JavaScript file in
I am designing a product management system. I am wondering the best way to
I'm wondering the best way to separate my plugin's templates from the actual logic.
I'm wondering the best way to start a pthread that is a member of
I was wondering the best way to upload file to a web server in
I'm new to SQL Server Reporting Services, and was wondering the best way to
Just wondering whats the best way to test Python CGI while developing a site?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.