Datagridview 中 输入字符串的格式不正确 如何处理

zhengyingcan 2010-04-30 09:11:34
dr["dec1"] = dataGridView1.Rows[i].Cells["dec1"].Value;
每次运行到上面的代码就出现 "输入字符串的格式不正确。不能在 dec1 列中存储 <1.23>。所需类型是 Int32。" 的提示,其实dec1的 字符类型 是decmical(18,2), 不知道为什么说所需类型是Int的 ,奇怪奇怪. 我实在是找不到原因
...全文
881 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhengyingcan 2010-05-03
  • 打赏
  • 举报
回复
那就这样子吧
wangwenzhuang 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lhaiyue7 的回复:]
不太清楚,1.23不是float类型吗?
[/Quote]
这样虽然不会报错了,但是值类型不对,赋不上的,
出错的原因可能是DatagridView的DataSorce已经绑定,绑定这一列的类型为Int,而你要赋值小数,肯定是不行的,
解决方法,你可以将DatagridView的数据源绑定的这一列设置为小数
walkghost 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zhengyingcan 的回复:]

foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["inta"].FormattedValue.ToString() != "" && row.Cells["cinvname"].FormattedValue.ToString() !=……
[/Quote]

这是两个string变量之间的引用赋值,同种类型,当然不需要转换。
zhengyingcan 2010-04-30
  • 打赏
  • 举报
回复
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["inta"].FormattedValue.ToString() != "" && row.Cells["cinvname"].FormattedValue.ToString() != "" && row.Cells["cinvstd"].FormattedValue.ToString() != "")
{
DataRow dr = dt1.NewRow();
dr["socode"] = socode;//销售单号
dr["inta"] = row.Cells["inta"].Value;
dr["sasa"] = row.Cells["sasa"].Value;
dr["cinvcode"] = row.Cells["cinvcode"].Value;
dr["cinvname"] = row.Cells["cinvname"].Value;
dr["cinvstd"] = row.Cells["cinvstd"].Value;
dr["sasb"] = row.Cells["sasb"].Value;
dr["intb"] = row.Cells["intb"].Value;
dr["sasc"] = row.Cells["sasc"].Value;
dr["sasd"] = row.Cells["sasd"].Value;
dr["sase"] = row.Cells["sase"].Value;
dr["sasf"] = row.Cells["sasf"].Value;
dr["sasg"] = row.Cells["sasg"].Value;
dr["money1"] = row.Cells["money1"].Value;
dr["money2"] = row.Cells["money2"].Value;
dt1.Rows.Add(dr);
}
}
为什么这样子写就不需要转换呢
walkghost 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhengyingcan 的回复:]

引用 9 楼 anlianganl 的回复:
C# code
int.Parse(dataGridView1.Rows[i].Cells["dec1"].Value);

我要的是decimal,有小数位的,能用这个方法转换吗,会不会最终没有小数位了呢
[/Quote]
decimal.Parse(dataGridView1.Rows[i].Cells["dec1"].Value);
zhengyingcan 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 anlianganl 的回复:]
C# code
int.Parse(dataGridView1.Rows[i].Cells["dec1"].Value);
[/Quote]
我要的是decimal,有小数位的,能用这个方法转换吗,会不会最终没有小数位了呢
anlianganl 2010-04-30
  • 打赏
  • 举报
回复
int.Parse(dataGridView1.Rows[i].Cells["dec1"].Value);
zhengyingcan 2010-04-30
  • 打赏
  • 举报
回复
我之前都不需要转换都行,奇怪为什么这次就不行.
zhengyingcan 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xiaoyuan245437 的回复:]
dataGridView1.Rows[i].Cells["dec1"].Value
这个需要转换一下 `
[/Quote]
如何转换呢,
Comeonzhou 2010-04-30
  • 打赏
  • 举报
回复
convert.todecimal(dataGridView1.Rows[i].Cells["dec1"].Value);
Xiaoyuan245437 2010-04-30
  • 打赏
  • 举报
回复
dataGridView1.Rows[i].Cells["dec1"].Value
这个需要转换一下 `
jianshao810 2010-04-30
  • 打赏
  • 举报
回复
dataGridView1.Rows[i].Cells["dec1"].Value;
转换下咯。他都提示这样做啦
songjd 2010-04-30
  • 打赏
  • 举报
回复
dataGridView1.Rows[i].Cells["dec1"].Value这个是object类型吧?你转换下试试
ginni215 2010-04-30
  • 打赏
  • 举报
回复
在DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs anError)
里面进行提示或者其他的处理就可以了~
笨笨7 2010-04-30
  • 打赏
  • 举报
回复
不太清楚,1.23不是float类型吗?

111,113

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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