开发者_如何学Goint 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.
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.
Use TryParse
to prevent an error:
int val = 0;
int.TryParse(lstvRecordsCus.SelectedItems[0].SubItems[0].Text, out val);
精彩评论