Select item from a Listview

M

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
 
G

Galcho[MCSD.NET]

M

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'
 
M

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 said:
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 said:
Use .SelectedItems[0] property and you will get ListViewItem object
http://msdn.microsoft.com/library/e...sFormsListViewItemMembersTopic.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top