想要在网格视图中绑定对象内的对象。这可能吗?

时间:2012-02-06 13:15:54

标签: c# asp.net gridview datasource

我的班级结构是

public class Listings
{
    public string id { get; set; }
    public string title { get; set; }

    public ListingsImages[] images;
}

public class ListingsImages
{ 
    public string src { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string alt { get; set; }
    public string num { get; set; }
    public string size { get; set; }
}

我想绑定我的网格视图,我想绑定为

List<Listings> p = getData(); //returns list of Listings
gv.DataSource = p;
gv.DataBind();

我的网格视图代码是

<asp:GridView ID="gv" runat="server" AutoGenerateColumns=false>
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%# Eval("id") %>
                        &nbsp&nbsp&nbsp&nbsp&nbsp
                        <%# Eval("title") %>
                        &nbsp&nbsp&nbsp&nbsp&nbsp
                        <%# Eval(?????)%>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

一个清单对象包含10张图片!现在的问题是我想显示所有列表对象的第6张图片然后?? 我试过了

 <%# Eval("images[5].src")%>

但它给了我错误!请回答我应该解决的问题? (仅使用一个gridview和一个数据源

1 个答案:

答案 0 :(得分:1)

是的,您可以通过使用RowDataBound()网格事件来做到这一点............

protected GridView_RowDataBound(object sender,
  GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
            //e.row.DataItem -- points to data / object which is going to be bind with the row 
     }
}