Hi, I have a few questions which I need answering please.

I have a ListView and I need to get the text in the cell which is clicked. At the moment i have this but it only works for the first cell which is clicked and throws an ArgumentOutOfRangeException if I click any more.

private void listView1_SelectedIndexChanged(object sender, EventArgs e)

            for (int i = 0; i < programmes.Count; i++)
                if (listView1.SelectedItems[0].SubItems[0].Text.ToString().Equals(programmes[i].Title))
                    MessageBox.Show("Title: " + programmes[i].Title + "\nStart Date: " + programmes[i].Start_d + "\nStart Time: " + programmes[i].Start_t +
                            "\nEnd Time: " + programmes[i].End_t + "\nChannel: " + getChannel() + "\nRepeat: " + programmes[i].Repeat);

Someone suggested using linq but I get the same error.

Secondly, what is the correct way to populate a ListView? At the moment i have this:

for (int i = 0; i < programmes.Count(); i++)
                    string[] arr = { programmes[i].Start_t.TimeOfDay.ToString(), programmes[i].End_t.TimeOfDay.ToString(), programmes[i].Repeat };

It works but its slightly annoying, Thanks.

if (listView1.SelectedItems[0].SubItems[0].Text.ToString()) //...

Items or SelectedItems means 1st column, SubItems are 2nd and more columns, depends which index do you specify in the brackets.
SubItems[0] is 1st column, same as Items[0], so you have 2 options:
1. for choosing 1st column value : .SelecteItems[0].Text;
2. or choosing 2nd (or more) column value: .SelectedItems[0].SubItems[1].Text;
2.1 for choosing a value from the 3rd column: .SelectedItems[0].SubItems[2].Text;

I hope we clerified about Items and SubItems properties of the listView.

Use listview1.Click event instead of selected text changed event

this.listView1.Click+=new System.EventHandler(listView1_Click);
 void listView1_Click(object sender, System.EventArgs e)
            //throw new System.NotImplementedException();

Thanks for the reply,

I removed the .SubItems[0] and it does the same thing as before, I can select an item once but then if I select others it still gives me the same ArgumentOutOfRange Exception.

If I understand you, you have 2+ columns in listView. And you would like to get the values from column1, column2, ... (if they are).
Am I right?

I would stronglly reccomend, what has abelLazm proposed you, to change the event. Do not use SelectedIndexChaged, becuase you will have only problems. Use Click event.
What is the problem with SelectedIndexChnaged event is that it wires twice for the row selection. When you clik the listView for the 1st time it fires ones - thats ok, but as soon as you click it for the 2nd time, it will fire for the 1st clicked row, and for the 2nd time for the newly clicked row. So it can make your life very miserable, if oyur dont know how to handle it.

As said, use Click event and all will be fine - its the same thing.

I did an example code how to get items and subitems from lisrView:

public Form1()

            listView1.Columns.Add("Column1", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("Column2", -2, HorizontalAlignment.Center);
            listView1.View = View.Details;
            listView1.FullRowSelect = true;

            //add some example rows:
            for (int i = 1; i < 5; i++)
                ListViewItem lvi = new ListViewItem(i.ToString() +".");
                lvi.SubItems.Add("Column 2 - " + i);

            listView1.Click += new EventHandler(listView1_Click);

        private void listView1_Click(object sender, EventArgs e)
            string col1 = listView1.SelectedItems[0].Text;
            string col2 = listView1.SelectedItems[0].SubItems[1].Text;

            MessageBox.Show("1st column: " + col1 + "\n2nd column: " + col2);

Sorry abelLazm I didn't see your post, thanks for the reply and thanks Mitja Bonca i'll have a go at using your code and i'll write back when i'm done. :)

This question has already been answered. Start a new discussion instead.