Reputation: 1395
I have a ListView in my aspx file like this:
<asp:ListView ID="ListView1" runat="server"></asp:ListView>
and then I have List like this:
List<string> mylist = new List<string>();
Im trying to populate the ListView like so:
foreach(string elem in mylist)
{
ListView1.Items.Add(new ListViewItem(elem));
}
But my compiler is telling me best overload match error & cannot convert string to ListViewItem. This is a asp.net 4.0 web app. Any suggestions?
Upvotes: 1
Views: 24359
Reputation: 8882
Try binding your ListView like the following:
code-behind
List<string> mylist = new List<string>() { "stealthy", "ninja", "panda"};
listView.DataSource = mylist;
listView.DataBind();
aspx
<asp:ListView ID="listView" runat="server">
<ItemTemplate>
<asp:Label Text="<%#Container.DataItem %>" runat="server" />
</ItemTemplate>
</asp:ListView>
Upvotes: 4
Reputation: 40789
ListViewItem does not have any constructor that takes a string.
Also ListView.Items is an IList<ListViewDataItem>
not a collection of ListViewItem
You probably want to use databinding rather than iteratively adding items anyway.
Upvotes: 2
Reputation: 38385
With a ListView
, you should be able to bind the data to the DataSource
of the control:
ListView1.DataSource = myList;
ListView1.DataBind();
Upvotes: 2