Reputation: 4088
Sorry for my bad English :(.
Hi, how do I add the items to a ListView that I've put in a List?
I've tried this:
listView1.Items.Add(pluginContainer);
But this doesn't seem to work :(.
I can't make a foreach loop because then it will take like 10 seconds for the ListView to be filled (I'm talking about 5000+ items).
This fixed it:
listView1.Items.AddRange(pluginContainer.ToArray());
Upvotes: 1
Views: 2396
Reputation: 29244
Try this:
public enum State
{
AL, GA, FL, SC, TN, MI
}
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public State State { get; set; }
// Converts properties to string array
public string[] ToListViewItem()
{
return new string[] {
ID.ToString("00000"),
Name,
State.ToString() };
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Setup list view column headings and widths
listView1.Columns.Add("ID", 48);
listView1.Columns.Add("Name", 300);
listView1.Columns.Add("State", 48);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Create a list
List<Person> list = new List<Person>();
// Fill in some data
list.Add(new Person() { ID=1001, Name="John", State=State.TN });
list.Add(new Person() { ID=1002, Name="Roger", State=State.AL });
list.Add(new Person() { ID=1003, Name="Samantha", State=State.FL});
list.Add(new Person() { ID=1004, Name="Kara", State=State.MI});
// Fill in ListView from list
PopulateListView(list);
}
void PopulateListView(List<Person> list)
{
listView1.SuspendLayout();
for(int i=0; i<list.Count; i++)
{
// create a list view item
var lvi = new ListViewItem(list[i].ToListViewItem());
// assign class reference to lvi Tag for later use
lvi.Tag = list[i];
// add to list view
listView1.Items.Add(lvi);
}
//This adjust the width of 1st column to fit data.
listView1.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
listView1.ResumeLayout();
}
}
Upvotes: 0
Reputation: 17002
If the items in your list are all of type ListViewItem, you can use AddRange. If they're not, you're going to have to either make ListViewItems out of them, or use a for loop.
In either case, you should strive to improve the performance of a ListView during addition of items, by first calling SuspendLayout on it. After you've added all its items, call ResumeLayout.
Upvotes: 1