Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Listview index problem

Listview index problem

Scheduled Pinned Locked Moved C#
helpdatabase
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GermanDM
    wrote on last edited by
    #1

    hi, in my application i have a listview with populated data. when i select a row i get my results but as soon as i want to select any other row i get the following error: InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index here is my code: private void listviewAcc_SelectedIndexChanged(object sender, EventArgs e) { _DataValues.AccountName = listviewAcc.SelectedItems[0].Text.ToString(); } no matter what i change the index to it will give me the same error. this only works the first time i selected a record but as soon as i make a second selection i get this error :confused: pls help thanks regards

    J 1 Reply Last reply
    0
    • G GermanDM

      hi, in my application i have a listview with populated data. when i select a row i get my results but as soon as i want to select any other row i get the following error: InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index here is my code: private void listviewAcc_SelectedIndexChanged(object sender, EventArgs e) { _DataValues.AccountName = listviewAcc.SelectedItems[0].Text.ToString(); } no matter what i change the index to it will give me the same error. this only works the first time i selected a record but as soon as i make a second selection i get this error :confused: pls help thanks regards

      J Offline
      J Offline
      Jasmine2501
      wrote on last edited by
      #2

      I had this problem too. When I debugged it, I noticed that it was calling my event handler twice if I changed from one selected item to another, but only once if I had nothing selected and I clicked one. This makes sense because when the selection changes from one item to another, it has to deselect the current item, which fires the 'Changed' event, and then it selects the new item, firing the event again. The first time the event gets called, there is no selection (SelectedItems has a size of 0), so you will get an InvalidArgument or possibly the "Object reference not set to an instance of an object" error. All you need to do is add a check to see if any items are selected. You can do that by examining the Count property of SelectedItems.

      "Quality Software since 1983!"
      http://www.smoothjazzy.com/ - see the "Programming" section for (freeware) JazzySiteMaps, a simple application to generate .Net and Google-style sitemaps!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups