locked
Get ListView Selected Item RRS feed

  • Question

  • Hey guys a small help needed here

    Actually i have a listView which contain some values, such as 

    A

    B

    C

    D

    E

     

    and i am currently retrieving its text using

    listView1.SelectedItems[0].Text;  and Display a message that this is selected item

    again when i select next item i get error

     

    InvalidArgument=Value of '0' is not valid for 'index'.

    Parameter name: index

     

    How to solve this

     

    Wednesday, August 10, 2011 6:56 AM

Answers

  • This situation happens when you have one value selected, and you select a different item.

    The selectedindexchanged event works like this:

    1. DEdelect the previous item

    2. Call the SelectedIndexChangedEvent

    3. SELECTS new item.

    4. Call the SelectedIndexChangedEvent.

    As you can see, when you change the item being selected, the SelectedIndexChanged fires twice.

    Now, when stage 1 fires, then there is really no selected item in the listview, count = 0,

    therefor there is no index to work with.

     

    The solutuion is to check if there are any selected items before using the index.

     

    Noam B.

     

     



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by amit_kumar Wednesday, August 10, 2011 8:10 AM
    Wednesday, August 10, 2011 7:49 AM

All replies

  • Hi

     

    Probably you are trying to access SelectedItems[0] when there are no selectedItems exists,

    You should have a check on SelectedItems count like 

    if(listView1.SelectedItems.Count > 0)
    {
     var txt =  listView1.SelectedItems[0].Text;
    }


     


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Proposed as answer by Hardz Tarrayo Wednesday, August 10, 2011 7:33 AM
    Wednesday, August 10, 2011 7:22 AM
  • Kris444, initially count is >0 hence i got A as Answer in mesagebox.

    after clicking ok, if i select B i get error

     




    Wednesday, August 10, 2011 7:27 AM
  • Hi 

     

    You can use a loop to loop all the item selected

    foreach (ListViewItem listv inthis.listView.Items)
    {
      // urtreatment and you can test in every item select in the loop
    }
    

    hope it helps


    Best Regards...Please mark as answer if my post is helpful
    Wednesday, August 10, 2011 7:48 AM
  • This situation happens when you have one value selected, and you select a different item.

    The selectedindexchanged event works like this:

    1. DEdelect the previous item

    2. Call the SelectedIndexChangedEvent

    3. SELECTS new item.

    4. Call the SelectedIndexChangedEvent.

    As you can see, when you change the item being selected, the SelectedIndexChanged fires twice.

    Now, when stage 1 fires, then there is really no selected item in the listview, count = 0,

    therefor there is no index to work with.

     

    The solutuion is to check if there are any selected items before using the index.

     

    Noam B.

     

     



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Marked as answer by amit_kumar Wednesday, August 10, 2011 8:10 AM
    Wednesday, August 10, 2011 7:49 AM
  • How do you want (actually where) to retreive selected item? On a listView click or on some button click?

    And do you select only one row in listView?

    you can try with:

    foreach (ListViewItem item in lvFiles.SelectedItems)
    {
       string textIn1stColumn = item.Text;
       stirng textIn2ndColumn = item.Subitems[1].Text;
       //and so on for coumns (change the index of subitems)
    }

     


    Mitja

    Wednesday, August 10, 2011 7:49 AM
  • Hi

    Which event you are writing this code? SelectedIndexChanged?

    probably you are trying to hit the code before selecting new node?


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Wednesday, August 10, 2011 7:58 AM
  • Got it super

     Noam B, it worked thanks. for explaining the logic

    thank you  Kris444

    thank you Mitja Nonca

    Thank you Yosrj

     

     

    Wednesday, August 10, 2011 8:12 AM
  • Kris444, initially count is >0 hence i got A as Answer in mesagebox.

    after clicking ok, if i select B i get error

     Khris`s code works 100%. You just dont do it right - Im sure.

    The only small glitch is maybe (especially if you show newly selected item in the messageBox) that when mesageBox appears, the newly selected item is not yet selected (visualy). But as soon as you press any button on the mesageBox, item gets selected. 

    But there for sure is not errors in the code - at least if you do it right.

    Check out this whole code, how you should do it:

        public Form1()
        {
          InitializeComponent();
          listView1.Columns.Add("col1", 100, HorizontalAlignment.Left);
          listView1.Columns.Add("col2", -2, HorizontalAlignment.Center);
          listView1.View = View.Details;
          listView1.FullRowSelect = true;
        
          //some example listView population:
          for (int i = 1; i < 6; i++)
          {
            ListViewItem lvi = new ListViewItem();
            lvi.Text = i.ToString();
            lvi.SubItems.Add("item " + i);
            listView1.Items.Add(lvi);
          }
    
          listView1.SelectedIndexChanged += new EventHandler(listView1_SelectedIndexChanged);
        }
    
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
          if (listView1.SelectedItems.Count > 0)
          {
            MessageBox.Show(listView1.SelectedItems[0].SubItems[1].Text);
          }
        }
    



    Mitja
    Wednesday, August 10, 2011 12:58 PM