gpt4 book ai didi

c# - 如何将 GridView 绑定(bind)到复杂对象

转载 作者:太空宇宙 更新时间:2023-11-03 19:25:44 24 4
gpt4 key购买 nike

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 的集合,当我想使用此代码将我的网格与此方法结果绑定(bind)时。

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

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

因为我的 GridView 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 类的属性。

我的问题可能看起来非常大,因为我不知道如何直接提问,所以任何人都会编辑这个问题以在小版本中表达我的观点。

最佳答案

如果你想获得 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();
}

关于c# - 如何将 GridView 绑定(bind)到复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8881028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com