Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating windows application using datagridview, I am getting error from cell values calculation time.

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



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#
Posted
Updated 22-Oct-16 8:00am
Comments
[no name] 22-Oct-16 14:01pm    
I seriously doubt that you have a DBNull value in your data grid.
ChrisHD22 22-Oct-16 18:41pm    
You are casting a boolean, that's why you get an invalid cast. You cannot convert a bool to a decimal.

Your code doesn't make sense at all.

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

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

Then you're OR'ing that with another bool expression:
C#
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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900