Hi
I've got a simple question I think.
I've got a simple question I think.
I've got a foreach loop:
You can see that this code inserts items to listview.
It does work, I just tested in writing the post.
But I'm just thinking how could I add the incremen int i to the name of the item.
Like this: item1, item2, item3.
I know I could do it with for loop.
Is it possible with foreach?
|
You can see that this code inserts items to listview.
It does work, I just tested in writing the post.
But I'm just thinking how could I add the incremen int i to the name of the item.
Like this: item1, item2, item3.
I know I could do it with for loop.
Is it possible with foreach?
Aaron ColenPosted Oct 12, 2010, 8:29 AM
item1 is also reset on every loop.
If you are trying to set a name to access the item later on you can set item1.Name.
int i = 0;
foreach (Car car in carrental.cars)
{
ListViewItem item = new ListViewItem(car.Name);
item.Name = "item" + i.ToString();
item.SubItems.Add(car.Owner);
...
listView1.Items.Add(item);
i++;
}
And then you can call item0, item1... by name later using listView1.Items["item1"]
Manikavelu VelayuthamPosted Oct 12, 2010, 4:47 AM
For Eg:-
you can replace this line
item1.SubItems.Add(car.Owner);
By
item1.SubItems.Add(car.Owner + i.ToString());