C# Convert.toDouble()为空值时报错

仰泳的鱼 2012-03-20 11:26:16
如题。

#region 实现最后一行合计函数
public void TotalRow(DataGridView dataGridview1)
{
DataGridViewRow dgr = dataGridview1.Rows[dataGridview1.Rows.Count - 1];
dgr.ReadOnly = true;
dgr.DefaultCellStyle.BackColor = System.Drawing.Color.Khaki;
dgr.Cells[0].Value = "合计";
double db;

for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{
db = Convert.ToDouble(dgr.Cells[4].Value) + Convert.ToDouble(dataGridview1.Rows[i].Cells[4].Value ??1);
dgr.Cells[4].Value = db.ToString("#0.00"); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }

}
}
#endregion


datagridview1控件最后一行取合计,累计加第五列的数据,可当单元格无值的时候会提示“输入字符串的格式不正确”,请教各位大侠。
...全文
872 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yujunkai_wushaoling 的回复:]

引用 3 楼 chinajiyong 的回复:
for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{
if(String.IsNullOrEmpty(dgr.Cells[4].Value.ToString()))
{
MessageBox.Show("数据为空!");
return;
}
db = Con……
[/Quote]
改成如下
if(String.IsNullOrEmpty(dgr.Cells[4].Value))
不要ToString()
仰泳的鱼 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chinajiyong 的回复:]
for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{
if(String.IsNullOrEmpty(dgr.Cells[4].Value.ToString()))
{
MessageBox.Show("数据为空!");
return;
}
db = Convert.ToD……
[/Quote]




if(String.IsNullOrEmpty(dgr.Cells[4].Value.ToString()))
未将对象引用设置到对象的实例
仰泳的鱼 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 chinajiyong 的回复:]
for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{
if(String.IsNullOrEmpty(dgr.Cells[4].Value.ToString()))
{
MessageBox.Show("数据为空!");
return;
}
db = Convert.ToD……
[/Quote]




if(String.IsNullOrEmpty(dgr.Cells[4].Value.ToString()))
未将对象引用设置到对象的实例
kiba518 2012-03-20
  • 打赏
  • 举报
回复
那是必然的啊 没有值就是空啊 你拿NULL+double当然报错啊 你判断一下 当他为空的时候 给他个0值 或者 当他为空不计算
EnForGrass 2012-03-20
  • 打赏
  • 举报
回复
for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{
if(String.IsNullOrEmpty(dgr.Cells[4].Value.ToString()))
{
MessageBox.Show("数据为空!");
return;
}
db = Convert.ToDouble(dgr.Cells[4].Value.ToString()) + Convert.ToDouble(dataGridview1.Rows[i].Cells[4].Value ??1);
dgr.Cells[4].Value = db.ToString("#0.00"); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }

}
仰泳的鱼 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
做验证啊,先判断dgr.Cells[4].Value是不是空,或者是否符合转换规则
[/Quote]
能详细点么?我是新手。
用if在前面判断?符合转换规则指的是?
bdmh 2012-03-20
  • 打赏
  • 举报
回复
做验证啊,先判断dgr.Cells[4].Value是不是空,或者是否符合转换规则
稻庄 2012-03-20
  • 打赏
  • 举报
回复
10L‘s right answer
仰泳的鱼 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chenymimi 的回复:]
public void TotalRow(DataGridView dataGridview1)
{
DataGridViewRow dgr = dataGridview1.Rows[dataGridview1.Rows.Count - 1];
dgr.ReadOnly = true;
dgr.DefaultCellStyle.BackColor = System.Draw……
[/Quote]
不过要做点小修改
db = Convert.ToDouble(Convert.ToString(dgr.Cells[4].Value) == "" ? 0 : dgr.Cells[4].Value) + Convert.ToDouble(Convert.ToString(dataGridview1.Rows[i].Cells[4].Value) == "" ? 0 : dataGridview1.Rows[i].Cells[4].Value);

因为dgr.Cells[4].Value是Object,要先转为string。
laichunlin 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 laichunlin 的回复:]

for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{
double value1="";
double value……
[/Quote]
刚变量定义错了,改为:
double value1=0;
double value2=0;
laichunlin 2012-03-20
  • 打赏
  • 举报
回复
for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{
double value1="";
double value2="";
if(!string.isNullOrEmpty(dgr.Cells[4].Value))
{
Convert.ToDouble(dgr.Cells[4].Value)
}
if(!dataGridview1.Rows[i].Cells[4].Value))
{
value2=Convert.ToDouble(dataGridview1.Rows[i].Cells[4].Value ??1
}
db = value1+value2;
dgr.Cells[4].Value = db.ToString("#0.00"); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }

}
仰泳的鱼 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chenymimi 的回复:]
public void TotalRow(DataGridView dataGridview1)
{
DataGridViewRow dgr = dataGridview1.Rows[dataGridview1.Rows.Count - 1];
dgr.ReadOnly = true;
dgr.DefaultCellStyle.BackColor = System.Draw……
[/Quote]
+1 这种方式挺简单实用的,谢谢。
仰泳的鱼 2012-03-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chinajiyong 的回复:]
引用 6 楼 yujunkai_wushaoling 的回复:

引用 3 楼 chinajiyong 的回复:
for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{
if(String.IsNullOrEmpty(dgr.Cells[4].Value.ToString()))
{
MessageBox.Sh……
[/Quote]
if里面的条件去掉.tostring()后就直接报错了。
chenymimi 2012-03-20
  • 打赏
  • 举报
回复
public void TotalRow(DataGridView dataGridview1)
{
DataGridViewRow dgr = dataGridview1.Rows[dataGridview1.Rows.Count - 1];
dgr.ReadOnly = true;
dgr.DefaultCellStyle.BackColor = System.Drawing.Color.Khaki;
dgr.Cells[0].Value = "合计";
double db;

for (int i = 0; i < dataGridview1.Rows.Count - 1; i++)
{
try
{

//
db = Convert.ToDouble(dgr.Cells[4].Value==""? 0:dgr.Cells[4].Value) + Convert.ToDouble(dataGridview1.Rows[i].Cells[4].Value==""?0:dataGridview1.Rows[i].Cells[4].Value);

dgr.Cells[4].Value = db.ToString("#0.00"); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }

}
}
porschev 2012-03-20
  • 打赏
  • 举报
回复

先对dgr.Cells[4].Value做非空验证

然后再对它进行转换操作
porschev 2012-03-20
  • 打赏
  • 举报
回复

先对dgr.Cells[4].Value做非空验证

再对它进行转换操作

110,535

社区成员

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

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

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