views:

33

answers:

2
int val = lstvRecordsCus.SelectedItems[0].SubItems[0];

The returned value is an int in a string datatype. I need the right hand side to return the value in int instead of string.

I tried Convert.ToInt32, it didn't work. Any idea?

All the values that comes from SelectedItems[0].SubItems[0] is of type int.

+6  A: 

Actually lstvRecordsCus.SelectedItems[0].SubItems[0] does not return a string, it returns a ListViewSubItem object which has a .Text property that you can convert to an integer.

int val = Int32.Parse(lstvRecordsCus.SelectedItems[0].SubItems[0].Text);

The reason that it probably appeared as a string if you were looking at it in the debugger is that ListViewSubItem overrides the ToString method which the debugger will use to represent an object in the watch window or info tips.

Josh Einstein
A: 

follow up question regarding this typecast. Does the following cast work?:

int val = (Int32)lstvRecordsCus.SelectedItems[0].SubItems[0].Text;

If not, please explain why =)

Marthin
It won't work and the reason why is because you can't cast a string to an integer.
Josh Einstein