指定的强制转换无效C# [英] Specified cast is not valid C#

查看:505
本文介绍了指定的强制转换无效C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用datagridview创建Windows应用程序,我从单元格值计算时间收到错误。



I am creating windows application using datagridview, I am getting error from cell values calculation time.

meters += Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value == DBNull.Value ||
                   (Decimal)this.dataGridView1.CurrentRow.Cells[1].Value == 0);







指定演员表无效C#



如何解决上述错误任何人都给我一些想法。



我尝试过:



指定演员表无效C#




Specified Cast is not Valid C#

How to solve above error any one give me some ideas.

What I have tried:

Specified Cast is not Valid C#

推荐答案

您的代码完全没有意义。



您正在尝试将bool表达式(true / false值)转换为小数:

Your code doesn't make sense at all.

You're trying to convert a bool expression (true/false value) to a decimal:
(Decimal)this.dataGridView1.CurrentRow.Cells[1].Value == 0);



你根本不需要这个。



然后你用另一个bool表达式进行OR运算:


You also don't need the "this" in there at all.

Then you're OR'ing that with another bool expression:

dataGridView1.Rows[i].Cells[1].Value == DBNull.Value





然后你试图将结果转换为十进制,这是行不通的。



您将不得不解释您希望此代码执行的操作。您发布的内容非常混乱,无法确定您要执行的操作。



And then you're trying to convert the result to a Decimal, which won't work.

You're going to have to explain what you want this code to do. What you've posted is so confused, it's impossible to figure out what you're trying to do.


ToDecimal内的值将被评估为布尔值。如果我错了,请纠正我。



所以,问题就是这样。确保你做了很好的转换。
The value inside the ToDecimal will be evaluate as a boolean, it seems. Correct me if I am wrong.

So, the issue is coming like that. Make sure you are doing a good conversion.


这篇关于指定的强制转换无效C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆