Binding Nested GridView to structure.
Objects:
namespace MIHotel_BE
{
public class HabitacionBE
{
public Int16 idHabitacion { get; set; }
public Int16 tipo { get; set; }
public Int16 estado { get; set; }
public Int16 piso { get; set; }
public String disponibilidad { get; set; }
public Int16 Fk_Empleado {get; set;}
public Int16 Fk_Categoria { get; set; }
public Int16 Fk_Reserva { get; set; }
public String nombrePiso { get; set; }
// Propiedad para representar la categoría
public CategoriaBE Categoria { get; set; }
}
public class CategoriaBE
{
public Int16 idCategoria { get; set; }
public String descripcion { get; set; }
public String caracteristicas { get; set; }
public Single tarifa { get; set; }
}
}
namespace MIHotel_BE
{
public class HabitacionBE
{
public Int16 idHabitacion { get; set; }
public Int16 tipo { get; set; }
public Int16 estado { get; set; }
public Int16 piso { get; set; }
public String disponibilidad { get; set; }
public Int16 Fk_Empleado {get; set;}
public Int16 Fk_Categoria { get; set; }
public Int16 Fk_Reserva { get; set; }
public String nombrePiso { get; set; }
// Propiedad para representar la categoría
public CategoriaBE Categoria { get; set; }
}
public class CategoriaBE
{
public Int16 idCategoria { get; set; }
public String descripcion { get; set; }
public String caracteristicas { get; set; }
public Single tarifa { get; set; }
}
}
5 Replies
<asp:GridView ID="grvHabitacion" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" OnPageIndexChanging="grvHabitacion_PageIndexChanging" PageSize="15" ShowHeaderWhenEmpty="True" Width="483px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="tipo" HeaderText="Tipo" />
<asp:BoundField DataField="estado" HeaderText="Estado" />
<asp:BoundField DataField="disponibilidad" HeaderText="Disponibilidad" />
<asp:TemplateField HeaderText="Categoria">
<ItemTemplate>
<asp:GridView ID="grvCategoria" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="tarifa" HeaderText="Tarifa" />
<asp:BoundField DataField="descripcion" HeaderText="Descripcion" />
<asp:BoundField DataField="caracteristicas" HeaderText="Caracteristicas" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
<asp:GridView ID="grvHabitacion" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" OnPageIndexChanging="grvHabitacion_PageIndexChanging" PageSize="15" ShowHeaderWhenEmpty="True" Width="483px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="tipo" HeaderText="Tipo" />
<asp:BoundField DataField="estado" HeaderText="Estado" />
<asp:BoundField DataField="disponibilidad" HeaderText="Disponibilidad" />
<asp:TemplateField HeaderText="Categoria">
<ItemTemplate>
<asp:GridView ID="grvCategoria" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="tarifa" HeaderText="Tarifa" />
<asp:BoundField DataField="descripcion" HeaderText="Descripcion" />
<asp:BoundField DataField="caracteristicas" HeaderText="Caracteristicas" />
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
protected void grvHabitacion_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grvCategoria = (GridView)e.Row.FindControl("grvCategoria");
HabitacionBE habitacion = (HabitacionBE)e.Row.DataItem;
grvCategoria.DataSource = new List<CategoriaBE> { habitacion.Categoria };
grvCategoria.DataBind();
}
}
protected void grvHabitacion_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grvCategoria = (GridView)e.Row.FindControl("grvCategoria");
HabitacionBE habitacion = (HabitacionBE)e.Row.DataItem;
grvCategoria.DataSource = new List<CategoriaBE> { habitacion.Categoria };
grvCategoria.DataBind();
}
}
Here are the functions called
Current result
Expecting this
CoreBehind