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. problem with list view selection.

problem with list view selection.

Scheduled Pinned Locked Moved C#
databasealgorithmshelpquestion
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.
  • S Offline
    S Offline
    santhosh padamatinti
    wrote on last edited by
    #1

    Hi, I am receiving a problem with my code. Actually in my code I have taken a list view. name is Listview1. Setting values and performing sorting every thing fine. But when I am trying to display selected value like bellow.

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    this.label1.Text = listView1.SelectedItems[0].ToString();
    }

    In first click it showing correct value. When clicking the other It throwing exception "System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index". How can i solve this problem. Only my intention is it has to display selected value in list view. How it is possible please show me the solution If any one knows this problem Thanks in advance.........

    sampath-padamatinti

    E 1 Reply Last reply
    0
    • S santhosh padamatinti

      Hi, I am receiving a problem with my code. Actually in my code I have taken a list view. name is Listview1. Setting values and performing sorting every thing fine. But when I am trying to display selected value like bellow.

      private void listView1_SelectedIndexChanged(object sender, EventArgs e)
      {
      this.label1.Text = listView1.SelectedItems[0].ToString();
      }

      In first click it showing correct value. When clicking the other It throwing exception "System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index". How can i solve this problem. Only my intention is it has to display selected value in list view. How it is possible please show me the solution If any one knows this problem Thanks in advance.........

      sampath-padamatinti

      E Offline
      E Offline
      EddieRich
      wrote on last edited by
      #2

      You are half way there. You need to add one more line. The problem is that when you select a new item, the old item becomes de-selected, and so the "SelectedIndexChanged" event is fired when there are no items selected. Then, the item you clicked becomes selected, and the "SelectedIndexChanged" event is fired again. Try this in your code ...

      	private void listView1\_SelectedIndexChanged(object sender, EventArgs e) {
      		if (listView1.SelectedItems.Count > 0)
      			label1.Text = listView1.SelectedItems\[0\].ToString();
      	}
      
      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