Click here to Skip to main content
14,970,345 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My code as follows
C#
    private void Form1_Load(object sender, EventArgs e)
      {
        listBox1.Items.Add("have a nice day");
        listBox1.Items.Add("nice day");
        listBox1.Items.Add("day");
      }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
       foreach (object item in listBox1.Items)
 {
      string km = item.ToString();
      listView1.Items.Add(km);
}
   }

i want to display the Listbox items into Listview.
But my above code is not working showing error as follows;


Argument 1: cannot convert from 'string' to 'System.Web.UI.WebControls.ListViewDataItem
The best overloaded method match for
C#
'System.Collections.Generic.ICollection<system.web.ui.webcontrols.listviewdataitem>.Add(System.Web.UI.WebControls.ListViewDataItem)' has some invalid arguments	


what is the problem in my code.

Regards,
Narasiman P.
Posted
Updated 24-Dec-13 20:24pm
v2

You should use ListView[^] of System.Windows.Controls Namespace not System.Web.UI.WebControls.

Refer- Add item to Listview control[^]
For adding one item, you should make an array and then initialize one ListViewItem object.
C#
string[] row = { textBox1.Text, textBox2.Text, textBox3.Text };

var listViewItem = new ListViewItem(row); 
listView1.Items.Add(listViewItem);
You declared it as string try to insert data type of ListViewItem

C#
foreach (object item in listBox1.Items)
     {
               
          string km = item.ToString();
          ListViewItem itemToAdd = new ListViewItem(km)
          listView1.Items.Add(itemToAdd);
    }
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900