如何将网格视图绑定到复杂对象

时间:2012-01-16 13:57:35

标签: c# linq linq-to-sql gridview

public class BE_RoomRateService
{
    public IQueryable<RoomRateWithRoomType> RoomRateForAdmin()
    {
                var query= from rate in db.BE_RoomRates 
                           join room in db.BE_Rooms
                           on rate.RoomId equals room.RoomId
                           where rate.Status!=3 && room.Status!=3
                           select new RoomRateWithRoomType()
                           {
                               RoomRate = rate,
                               RoomType = room.RoomType

                           };
                return query;
     }
}

public class RoomRateWithRoomType
{
            public BE_RoomRate RoomRate { get; set; }
            public string RoomType { get; set; }
}

此处RoomRateForAdmin现在返回RoomRateWithRoomType的集合,此时我想使用此代码将此网格与此方法结果绑定。

grdData.DataSource = bs.RoomRateForAdmin();
grdData.DataBind();

然后它给了我错误:
BussinessLogic.Data.RoomRateWithRoomType'不包含名称为“SingleRate”的属性。

我的网格视图的aspx页面如下:

<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false" GridLines="None"
Width="900" OnRowDataBound="grdData_RowDataBound" OnRowCommand="grdData_RowCommand" >
    <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                Room Type</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblRoomType" runat="server" Text='<%# Eval("RoomType")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Single Rate</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblSingleRate" runat="server" Text='<%# Eval("SingleRate")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField>
            <HeaderTemplate>
                Double Rate</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblDoubleRate" runat="server" Text='<%# Eval("DoubleRate")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Start Date</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblStartDate" runat="server" Text='<%# Eval("StartDate")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                End Date</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblEndDate" runat="server" Text='<%# Eval("EndDate")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField>
            <HeaderTemplate>
                Status</HeaderTemplate>
            <ItemTemplate>
                    <asp:ImageButton ID="imgStatus" runat="server" CommandName="Status" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Edit</HeaderTemplate>
            <ItemTemplate>
                <asp:ImageButton ID="imgEdit" runat="server" ToolTip="Click here to view/edit this Events"
                    AlternateText="Edit" CommandName="EditRow" CommandArgument='<%# Bind("RoomRateId") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Delete</HeaderTemplate>
            <ItemTemplate>
                <asp:ImageButton ID="imgDelete" runat="server" ToolTip="Click here to delete this Events"
                    AlternateText="Delete" CommandName="Del" CommandArgument='<%# Bind("RoomRateId") %>' />
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

我知道我无法直接获取RoomRate表属性,但我无法获得如何获取BE_RoomRate类的属性。

我的问题可能看起来很大,因为我不知道如何直接问它所以任何人都会编辑这个问题以获得我的小版本。

2 个答案:

答案 0 :(得分:4)

如果您想获得RoomRate SingleRate。然后而不是使用

Eval("SingleRate")

使用

DataBinder.Eval(Container.DataItem, "RoomRate.SingleRate")

请参阅此小例子(背后的代码)

public class SomeObject
{
    public Version Version { get; set; }
    public string Description { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
    var ls = new List<SomeObject>()
                            {
                                new SomeObject() {Description = "Test", Version = new Version(1, 1)},
                                new SomeObject() {Description = "Test2", Version = new Version(2, 1)}
                            };
    gv.DataSource = ls;
    gv.DataBind();
}

aspx:

<asp:GridView ID="gv" runat="server" Width="100%">
        <Columns>
            <asp:TemplateField>
            <HeaderTemplate>
                Major</HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblRoomType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Version.Major") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>

因为DataKeyNames属性是字符串而不是表达式,所以我建议你为它创建一个属性。所以你的对象看起来像这样:

public class RoomRateWithRoomType
{
      public BE_RoomRate RoomRate { get; set; }
      public string RoomType { get; set; }
      public int RoomRateId { get; set; }
}

然后在网格中你可以使用:

DataKeyNames="RoomRateId"

为了避免每行一次数据库调用,请更改:

public IQueryable<RoomRateWithRoomType> RoomRateForAdmin()
{
            var query= from rate in db.BE_RoomRates 
                       join room in db.BE_Rooms
                       on rate.RoomId equals room.RoomId
                       where rate.Status!=3 && room.Status!=3
                       select new RoomRateWithRoomType()
                       {
                           RoomRate = rate,
                           RoomType = room.RoomType

                       };
            return query;
 }

public IEnumerable<RoomRateWithRoomType> RoomRateForAdmin()
        {
                    var query= from rate in db.BE_RoomRates 
                               join room in db.BE_Rooms
                               on rate.RoomId equals room.RoomId
                               where rate.Status!=3 && room.Status!=3
                               select new RoomRateWithRoomType()
                               {
                                   RoomRate = rate,
                                   RoomType = room.RoomType

                               };
                    return query.ToList();
         }

答案 1 :(得分:2)

我认为您最好的选择是返回一个对象(可以是匿名的),其中包含绑定网格所需的全部属性。