By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,825 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,825 IT Pros & Developers. It's quick & easy.

Select item from a Listview

P: n/a
M K
Using VS 2005

I have a listview that displays:

number, name, details (in that order)

when the user selects a row (single select) i want to retrieve each column
in a variable.

number is an int
name is a string
details is a string

i have searched the net and could not locate an example.

I am not trying to do this from the listview control but rather from a
button.

any help greatly appreciated.

Mark
May 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Use .SelectedItems[0] property and you will get ListViewItem object
http://msdn.microsoft.com/library/en...asp?frame=true
use .Subitems(0) - for number
use .Subitems(1) - for name
use .Subitems(2) - for details
ListViewItem lvItem = list..SelectedItems[0];
int number = (int)lvItem.SubItems[0] ;
string name = (string)lvItem.SubItems[1] ;
string details = (string)lvItem.SubItems[2] ;
I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 17 '06 #2

P: n/a
M K
thats progress but now i get the following:

Error 1 Cannot convert type
'System.Windows.Forms.ListViewItem.ListViewSubItem ' to 'int'
Error 1 Cannot convert type
'System.Windows.Forms.ListViewItem.ListViewSubItem ' to 'string'
Error 1 Cannot convert type
'System.Windows.Forms.ListViewItem.ListViewSubItem ' to 'string'

"Galcho[MCSD.NET]" <ga****@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Use .SelectedItems[0] property and you will get ListViewItem object
http://msdn.microsoft.com/library/en...asp?frame=true
use .Subitems(0) - for number
use .Subitems(1) - for name
use .Subitems(2) - for details
ListViewItem lvItem = list..SelectedItems[0];
int number = (int)lvItem.SubItems[0] ;
string name = (string)lvItem.SubItems[1] ;
string details = (string)lvItem.SubItems[2] ;
I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

May 18 '06 #3

P: n/a
M K
This did work.. i figured it out.. thanks for your help.

ListViewItem lvItem = lvTodo.SelectedItems[0];

int number = Convert.ToInt32(lvItem.SubItems[0].Text);

string name = lvItem.SubItems[1].Text;

string details = lvItem.SubItems[3].Text;

"M K" <sk*******@earthlink.net> wrote in message
news:Dl****************@newsread2.news.pas.earthli nk.net...
thats progress but now i get the following:

Error 1 Cannot convert type
'System.Windows.Forms.ListViewItem.ListViewSubItem ' to 'int'
Error 1 Cannot convert type
'System.Windows.Forms.ListViewItem.ListViewSubItem ' to 'string'
Error 1 Cannot convert type
'System.Windows.Forms.ListViewItem.ListViewSubItem ' to 'string'

"Galcho[MCSD.NET]" <ga****@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
Use .SelectedItems[0] property and you will get ListViewItem object
http://msdn.microsoft.com/library/en...asp?frame=true
use .Subitems(0) - for number
use .Subitems(1) - for name
use .Subitems(2) - for details
ListViewItem lvItem = list..SelectedItems[0];
int number = (int)lvItem.SubItems[0] ;
string name = (string)lvItem.SubItems[1] ;
string details = (string)lvItem.SubItems[2] ;
I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com


May 18 '06 #4

P: n/a
sorry, I missed to add .Text property to code

I am glad that I helped you

Regards.
Galin Iliev[MCSD.NET]
www.galcho.com

May 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.