Reputation: 1453
I have a listview inside a templatefield in an asp.net web application.
I need it to display the contents of a collection, 1 per row.
How can I do that?
I want the the table of the ListView to have multiple rows. 1 for each item in the list. So for example:
Breeding Group | Breeding Group Role | Default
AAA 1111 F
BBB 2222 T
CCC 3333 F
And that is for each 'Crop'
Is that possible?
Current Code:
Class:
[Serializable()]
public class UserCrops
{
public UserCrops(string _Crop, List<string> _BG, List<string> _BGR, List<bool> _Default)
{
Crop = _Crop;
BG = _BG;
BGR = _BGR;
Default = _Default;
}
public string Crop { get; set; }
public List<string> BG { get; set; }
public List<string> BGR { get; set; }
public List<bool> Default { get; set; }
}
Binding:
protected void GVUserCrops_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ListView lv = (ListView)e.Row.FindControl("lvBGs");
lv.DataSource = finalSelection;
lv.DataBind();
}
}
GridView:
<asp:GridView ID="GVUserCrops" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDataBound="GVUserCrops_RowDataBound"
Width="634px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Crop" HeaderText="Crop" />
<asp:TemplateField HeaderText="BG/BGR">
<ItemTemplate>
<asp:ListView ID="lvBGs" runat="server">
<LayoutTemplate>
<table style="border: solid 2px #336699;" cellspacing="0" cellpadding="3" rules="all">
<tr style="background-color: #336699; color: White;">
<th>
Breeding Group
</th>
<th>
Breeding Group Role
</th>
<th>
Default
</th>
</tr>
<tbody>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("BG")%>
</td>
<td>
<%# Eval("BGR")%>
</td>
<td>
<%# Eval("Default")%>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color: #dadada;">
<td>
<%# Eval("BG")%>
</td>
<td>
<%# Eval("BGR")%>
</td>
<td>
<%# Eval("Default")%>
</td>
</tr>
</AlternatingItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Edit:
Screenshot for list:
Edit2:
Sketch for what I am trying to accomplish.
Please excuse the crude drawings. I am trying to get the list called finalSelection(in Sketch#1, 8 items) to appear like the Gridview in Sketch#2. Could probably do it much easier using cell spanning but seems like cheating. So I made a gridview with 1 bound column 'Crop' and another column (templatecolumn) I then put a listview control with a layout like a table(which i love) Problem is when i assign listview.datasource to the finalSelection List is won't show like I am hoping (which seems logical) What can i do to accomplish my goal? :)
Upvotes: 0
Views: 469
Reputation: 2755
Replace the <%# Eval("BG")%>
with <%# Container.DataItem %>
EDIT: After screen shot from OP.
Seems your DataSource is wrong. You are referring the class that the list is contained in.
Try this:
lv.DataSource = finalSelection.BG;
Upvotes: 1