为啥类型转换总出错阿

wolai66 2012-03-01 11:53:16

int temp2 = Convert.ToInt32 (gv.Rows[i].Cells[12+reccount].Text);
其中gv.Rows[i].Cells[12+reccount].text是数字,用convert或者parse 转换的时候,总是出错。

System.FormatException: Input string was not in a correct format.

...全文
120 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvjianwu 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 junior_yuan 的回复:]

引用 6 楼 long4239589 的回复:
引用 5 楼 wolai66 的回复:
int temp2 = Int32.TryParse(gv.Rows[i].Cells[12 + reccount].Text, out temp2) ? temp2 : 0;
这个语句可以了,
另外,怎么用乘法?
int temp1 = int.Parse (gv.Rows[i].Cells[1……
[/Quote]是不是有空格等
小孩快跑 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 long4239589 的回复:]
引用 5 楼 wolai66 的回复:
int temp2 = Int32.TryParse(gv.Rows[i].Cells[12 + reccount].Text, out temp2) ? temp2 : 0;
这个语句可以了,
另外,怎么用乘法?
int temp1 = int.Parse (gv.Rows[i].Cells[12 + j].Text.ToString());
……
[/Quote]

+1
qydvip 2012-03-01
  • 打赏
  • 举报
回复
打断点看看gv.Rows[i].Cells[12+reccount].Text
的值
暖枫无敌 2012-03-01
  • 打赏
  • 举报
回复
int temp2 = Convert.ToInt32 (gv.Rows[i].Cells[12+reccount].Text);

红色标记写法有错误,你是根据reccount值在原先的12基础上继续加的是吧?不能这样写的

...
int index = 12+reccount;
int temp2 = Convert.ToInt32 (gv.Rows[i].Cells[index].Text);
...
华生 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wolai66 的回复:]
int temp2 = Int32.TryParse(gv.Rows[i].Cells[12 + reccount].Text, out temp2) ? temp2 : 0;
这个语句可以了,
另外,怎么用乘法?
int temp1 = int.Parse (gv.Rows[i].Cells[12 + j].Text.ToString());
gv.Rows[i].Cells[15+……
[/Quote]把temp1和Temp2的值输出看下
wolai66 2012-03-01
  • 打赏
  • 举报
回复
int temp2 = Int32.TryParse(gv.Rows[i].Cells[12 + reccount].Text, out temp2) ? temp2 : 0;
这个语句可以了,
另外,怎么用乘法?
int temp1 = int.Parse (gv.Rows[i].Cells[12 + j].Text.ToString());
gv.Rows[i].Cells[15+reccount + j].Text =(temp1 * temp2).ToString() ;出错阿,temp1值没问题,验证过。
ycproc 2012-03-01
  • 打赏
  • 举报
回复
gv.Rows[i].Cells[12+reccount].Text

你自己知道 默认的这个是什么 样的数据格式吗?

kkun_3yue3 2012-03-01
  • 打赏
  • 举报
回复
int temp2 = Int32.TryParse(gv.Rows[i].Cells[12 + reccount].Text, out temp2) ? temp2 : 0;
EnForGrass 2012-03-01
  • 打赏
  • 举报
回复
gv.Rows[i].Cells[12+reccount].text换成
gv.Rows[i].Cells[12+reccount].Value
老毕 2012-03-01
  • 打赏
  • 举报
回复
自己打个断点,看一看gv.Rows[i].Cells[12+reccount].Text的值,多半不符合数字的格式要求
相当之稳重 2012-03-01
  • 打赏
  • 举报
回复
几种常用的转换方式。。拿int型举例。。

Convert.ToInt32(); //将object类型转换为int型

int.Parse(); //将string型转换为int型

(int)... //一般的类型转换,比如将浮点型数据转成整型。。
wolai66 2012-03-01
  • 打赏
  • 举报
回复
好了,改成todecimal了
wolai66 2012-03-01
  • 打赏
  • 举报
回复
Response.Write(gv.Rows[6].Cells[index ].Text.ToString().Trim());显示“1.0000


Response.Write(Convert.ToInt32 (gv.Rows[6].Cells[index ].Text.ToString().Trim())); 加上convert.toint32就报错System.FormatException: Input string was not in a correct format.

用parse也一样

fanxin_1999 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abbey 的回复:]
自己打个断点,看一看gv.Rows[i].Cells[12+reccount].Text的值,多半不符合数字的格式要求
[/Quote]

还是调试比较好。
烟波钓 2012-03-01
  • 打赏
  • 举报
回复
根据ls解决办法 试试如下代码

int temp2 = Int32.TryParse(gv.Rows[i].Cells[reccount].Text, out temp2) ? temp2+12: 12;

烟波钓 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 taomanman 的回复:]
int temp2 = Convert.ToInt32 (gv.Rows[i].Cells[12+reccount].Text);

红色标记写法有错误,你是根据reccount值在原先的12基础上继续加的是吧?不能这样写的

...
int index = 12+reccount;
int temp2 = Convert.ToInt32 (gv.Rows[i].Cells[index……
[/Quote]

感觉这才是正解了吧
kkun_3yue3 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wolai66 的回复:]

int temp2 = Int32.TryParse(gv.Rows[i].Cells[12 + reccount].Text, out temp2) ? temp2 : 0;
这个语句可以了,
另外,怎么用乘法?
int temp1 = int.Parse (gv.Rows[i].Cells[12 + j].Text.ToString());
gv.Rows[i].Cells[15……
[/Quote]
继续TryParse啊
int temp1 = Int32.TryParse (gv.Rows[i].Cells[12 + j].Text.ToString()) ? temp1 : 0;
EnForGrass 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 taomanman 的回复:]

int temp2 = Convert.ToInt32 (gv.Rows[i].Cells[12+reccount].Text);

红色标记写法有错误,你是根据reccount值在原先的12基础上继续加的是吧?不能这样写的

...
int index = 12+reccount;
int temp2 = Convert.ToInt32 (gv.Rows[i].Cells[inde……
[/Quote]
涉及到下标都要注意不要越界

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧