GridView 直接绑定复合对象属性
<asp:GridView ID="gvNews" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" SkinID="GridViewThemen" OnRowDataBound="gvNews_RowDataBound" OnRowCommand="gvNews_RowCommand"> <Columns> <asp:TemplateField HeaderText="" > <HeaderStyle HorizontalAlign="Center"></HeaderStyle> <ItemStyle Width="10px" HorizontalAlign="Center"></ItemStyle> <HeaderTemplate> <input id="chkAll" onclick="SelectAll(this, 99)" type="checkbox" runat="server" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkSelected" TabIndex="99" runat="server" type="checkbox"></asp:CheckBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText=""> <ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate> <EditItemTemplate> <%# Container.DataItemIndex + 1 %> </EditItemTemplate> <HeaderStyle Wrap="False" Width="10px" HorizontalAlign="Center"></HeaderStyle> </asp:TemplateField> <asp:TemplateField HeaderText="关键词" SortExpression=""> <HeaderStyle Width="190px" HorizontalAlign="Center" Wrap="False" /> <ItemTemplate> <%# Eval("Word") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="所属客户" SortExpression=""> <HeaderStyle Width="190px" HorizontalAlign="Center" Wrap="False" /> <ItemTemplate> <%# Eval("Customer.CompanyName")%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="操作"> <HeaderStyle Width="30px" HorizontalAlign="Center" Wrap="False" /> <ItemStyle Width="30px" HorizontalAlign="Center"></ItemStyle> <ItemTemplate> <asp:ImageButton ID="imgbtnDetail" runat="server" CommandArgument='<%# Eval("Id") %>' SkinID="btnDetail" CommandName="Detail"></asp:ImageButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
public partial class BusKeyword : BaseEntity { #region Primitive Properties /// <summary> /// Gets or sets the Id /// </summary> public int Id { get; set; } /// <summary> /// Gets or sets the CustomerId /// </summary> public int CustomerId { get; set; } /// <summary> /// Gets or sets the Word /// </summary> public string Word { get; set; } /// <summary> /// Gets or sets the CreateTime /// </summary> public Nullable<System.DateTime> CreateTime { get; set; } /// <summary> /// Gets or sets the CreateById /// </summary> public Nullable<int> CreateById { get; set; } /// <summary> /// Gets or sets the ModifyTime /// </summary> public Nullable<System.DateTime> ModifyTime { get; set; } /// <summary> /// Gets or sets the ModifyById /// </summary> public Nullable<int> ModifyById { get; set; } #endregion #region Custom Properties public BusCustomer Customer { get { if (this.CustomerId != 0) return IoC.Resolve<IBusCustomerService>().GetById(this.CustomerId); return null; } } #endregion }
效果如下: