rahularyansharma
rahularyansharma

Reputation: 10755

how to bind the grid view to complex object

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

here RoomRateForAdmin return a collection of RoomRateWithRoomType now when i want to bind my grid with this method result with this code.

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

then it gives me error:
BussinessLogic.Data.RoomRateWithRoomType' does not contain a property with the name 'SingleRate'.

as my aspx page for grid view is as below :

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

i know that i cant get the RoomRate table properties directly, but i am not getting how to get the properties of BE_RoomRate class.

my question can be look very large as i dont know how to ask it directly so anybody will edit this question to get my point in small version.

Upvotes: 1

Views: 5151

Answers (2)

Arion
Arion

Reputation: 31239

If you want to get the RoomRate SingleRate. Then instead of using

Eval("SingleRate")

Use

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

See this small example (Code behind)

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

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

Because the DataKeyNames property is a string and not a expression I would suggest that you create a property for that. So your object looks like this:

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

And then in the grid you can use:

DataKeyNames="RoomRateId"

To avoid one database call per line please change this:

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

To

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

Upvotes: 4

Magnus
Magnus

Reputation: 46919

I think your best option here is to return an object (can be anonymous) that contains all the properties you need to bind the grid.

Upvotes: 2

Related Questions