0

Hello,
I have a bunch of strings and a ready listview but my problem is to insert those strings into the list view in column 2, while running.
I can't find any solution, I have only found tutorials on creating a list view and sub items via code, but i need to change the column while running.
Thanks.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by mcriscolo
0

You can search through the items in a ListView, by iterating through the "Items" collection. Further, each item has a SubItems collection. So, with code like this, you can look for an item in a ListView and do something with it. In this case, I have a ListView called "lvBatters" and I am try to locate a batter (value in txtBatter) and replace the item in column 2 of the ListView with the batting average (stored in sAvg - converted to a string earlier):

bool bFound = false;
    foreach (ListViewItem item in lvBatters.Items)
    {
        if (item.Text == txtBatter.Text)
        {
            item.SubItems[1].Text = sAvg;  // Set the batting average
            bFound = true;
            break;   // break out of the loop
        }
    }

    // If I don't find the batter in the list, add it!
    if (!bFound)
    {
        ListViewItem i = new ListViewItem(txtBatter.Text);
        i.SubItems.Add(sAvg);
        lvBatters.Items.Add(i);
    }

Keep in mind, "items.SubItems[0].Text" is the value in the first column!

Try that, and see what happens.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.