使用对象数据填充GridView

时间:2011-10-25 16:32:48

标签: c# asp.net gridview objectdatasource

想知道使用我的对象数据填充GridView的最佳方法。

我必须显示来自复杂对象Sale的产品列表,其结构如下:

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

我的网格应如下所示:

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

我应该为Sale对象实现ObjectDataSource吗?有没有更好的解决方案?


编辑2:让我试着说清楚:问题不在于如何进行显示配置。 我的问题是Sale对象从持久层返回到我的代码,这就是我不希望GridView直接访问数据库的原因。相反,它需要从我的Sale对象加载其所有数据,如何实现?


编辑:

请求网格标记:

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

到目前为止,丑陋的解决方案,使用OnRowDataBound(我想避免这种情况!):

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();
}

2 个答案:

答案 0 :(得分:3)

我建议将TemplateColumns与绑定表达式一起使用。您可以将GridView绑定到saleItems列表,并实现getter方法以在给定SaleItem实例的情况下呈现每个字段。例如,您的名称列可以定义如下:

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

使用自定义getter方法将访问详细信息移动到代码隐藏中可以做同样的事情:

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

不要忘记添加导入指令以便能够引用您的类型:

<%@ Import Namespace="YouNamespaceHere" %>

答案 1 :(得分:1)

  dataGridView1.DataSource = ToDataTable(SaleItems);

如果可以将复杂对象列表转换为数据表,则可以将数据直接绑定到dataGridView

 private DataTable ToDataTable(List<Sale> SaleItems)
    {
        DataTable returnTable = new DataTable("Sale");
        returnTable.Columns.Add(new DataColumn("Name"));
        returnTable.Columns.Add(new DataColumn("#"));
        returnTable.Columns.Add(new DataColumn("B. Address"));
        returnTable.Columns.Add(new DataColumn("S. Address"));
        returnTable.Columns.Add(new DataColumn("Configurations"));

        foreach (Sale item in SaleItems)
        {
            returnTable.AcceptChanges();
            DataRow row = returnTable.NewRow();
            row[0] = item.product.name;
            row[1] = item.quantity.ToString();
            row[2] = item.billingAddress.ToString();
            row[3] = item.billingAddress.ToString();
            StringBuilder sbConfigurations = new StringBuilder();

            foreach (BuyingConfiguration configurationItem in item.product.buyingConfigurations) {
               sbConfigurations.AppendFormat("{0}: {1}<br />", configurationItem.name, configurationItem.value);
            }
            row[4] = sbConfigurations.ToString();

            returnTable.Rows.Add(row);
        }
        return returnTable;
    }