1

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?

Bramble
  • 1,385
  • 11
  • 38
  • 55

3 Answers3

4

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>
KodeKreachor
  • 8,762
  • 10
  • 46
  • 63
2

With a ListView, you should be able to bind the data to the DataSource of the control:

ListView1.DataSource = myList;
ListView1.DataBind();
Metro Smurf
  • 35,510
  • 20
  • 101
  • 134
2

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.

John Weldon
  • 38,339
  • 11
  • 92
  • 126
  • 1
    It's easy to confuse the web control with the forms control of the same name (which does provide a constructor that takes a string) – Stefan H Mar 30 '12 at 01:54