Click here to Skip to main content
14,829,981 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hello friends I have an problem in my windows application.
I have one listview. when user select any row on that listview than that selected row contents must be display in others combo box.

What I have tried:

my code is here. but it will show error:

C#
lbl_id.Text = listView1.SelectedItems[0].Text;



I have done same task with data grid view on below code and its working . but its grid view and now I wanted it on list view
C#
txt_card.Text = (dataGridView1.Rows[index].Cells["column_name"].Value.ToString());
Posted
Updated 18-May-16 11:00am
v2
Comments
Animesh Datta 16-May-16 8:30am
   
what error comes ?
Karthik_Mahalingam 16-May-16 8:34am
   
what is the error message
CPallini 16-May-16 8:38am
   
Did you read (about the SelectedItems property):
Remarks
This property is meant to be used when SelectionMode does not equal Single. If the selection mode is Single the correct property to use is SelectedItem.
?
Maciej Los 16-May-16 13:15pm
   
Carlo, do you remember your answer to this question: http://www.codeproject.com/Questions/891890/select-only-one-item-in-listview?
Sergey Alexandrovich Kryukov 16-May-16 16:51pm
   
You cannot unconditionally do so, because it will throw exception when nothing is selected.
—SA

the error is as per below :
argument out of range exception
invalid argument=value of 0 is not a valid of index
   
Comments
Patrice T 18-May-16 16:45pm
   
this is not a solution
Use Improve question to update your question.
and delete.
Quote:
argument out of range exception
invalid argument=value of 0 is not a valid of index
This message have 2 possible origins.
-Either there was a fail and the variable do contain NULL instead of the expected object.
-Or the list object you try to access is smaller than you expect and you try access an element than do not exist.
C#
lbl_id.Text = listView1.SelectedItems[0].Text;

For example, what is the size of SelectedItems if there is no selection ?
You will have to check its size in case it is 0.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900