Edwin Torres
Edwin Torres

Reputation: 483

Change text of a listview

I have a quick question about listView's and how check if a ListView (which contains null items) has a certain string?

Here is the code which add to sepcific items (under column 5 in the listView). It basically checks if that item appears in Google search or not. If it does, it'll write yes to that specific row, if not it'll leave it blank:

string google2 = http.get("https://www.google.com/search?q=" + textBox1.Text + "");
                string[] embedid = getBetweenAll(vid, "type='text/html' href='http://www.youtube.com/watch?v=", "&feature=youtube_gdata'/>");

                for (int i = 0; i < embedid.Length; i++)
                {

                    if (google2.Contains(embedid[i]))
                    {
                        listView1.Items[i].SubItems.Add("Yes");
                        }
                    }

Now what I am trying to do is check if that certain column contains items that say Yes. If it does color them Green if not don't.

Here's the code for that:

 if (i.SubItems[5].Text.Contains("Yes"))
                        {
                            labelContainsVideo.ForeColor = System.Drawing.Color.Green;
                        }

My issue is I keep getting an error that says InvalidArgument=Value of '5' is not valid for 'index'.

My hunch is that there are null items in column 5 which might be messing it up but I dont know.

Any idea on how to fix this?

Upvotes: 1

Views: 1445

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

Check the Item to see if the SubItem Collection has the correct Number of values.

i.e.

int threshold = 5;

foreach (ListViewItem item in listView1.Items)
{
    if (item.SubItems.Count > threshold)
    {
        if (item.SubItems[5].Text.Contains("Yes"))
        {
            // Do your work here
        }
    }
}

Upvotes: 1

Related Questions