The specific error I've been getting is that there is no row at position 49, which pops up when I try to get the value of mydataset.Tables["mytable"].Rows[idNum]["CarID"].ToString();. Now, I can see a few things that could be causing this issue, I have absolutely know idea if Rows[idNum]["CarID"] is the correct syntax, and was very surprised to see that my guess worked, but it's still a very weird problem.推荐答案您需要获取Char的数值:int idNum = (int)Char.GetNumericValue(listBox1.SelectedItem.ToString()[0]); int idNum = listBox1.SelectedItem.ToString()[0]);返回字符'1'的ASCII(示例)为49.While int idNum = listBox1.SelectedItem.ToString()[0]); return the ASCII of Character '1' (example) which is 49.这是来自 Microsoft 中用于Convert.ToInt16的实际代码:This is the actual code from Microsoft for Convert.ToInt16:public static short ToInt16(char value){ // Some validations return (short)value;}的 Convert.ToInt16进行Explicit转换,这将获得该Character值的ASCII.Convert.ToInt16 for a char does an Explicit conversion, which gets the ASCII for that Character Value. 用于处理多个数字:string str = "37abcdef";string myStrNumber = Regex.Match(str, @"\d+").Value;int idNum2;if (myStrNumber.Length > 0) idNum2 = Convert.ToInt32(myStrNumber);else{ // Handle Error} 或者不使用正则表达式:string str = "37abcdef";string myStrNumber = "";for(int i = 0; i < str.Length; i++){ if (Char.IsNumber(str[i])) myStrNumber += str[i];}int idNum2;if (myStrNumber.Length > 0) idNum2 = Convert.ToInt32(myStrNumber);else{ // Handle Error} 这篇关于从字符串中获取字符会返回意外数字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 05:42