problem with list view selection.
-
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
-
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
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(); }