Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
double total = 0;

            foreach (DataGridViewRow row in gvpurchase.Rows)
            {
                if (!row.IsNewRow && row.Cells["paid"].Value != null)
                {
                    total += (double)row.Cells["paid"].Value;

                    //Convert.ToInt32(["paid"]);
                }
            }

            lbltotal.Text = total.ToString("N2");
        }




    }


What I have tried:

I get grid view sum amount in label for windows application, but i got a error Specified Cast is not valid, how to solve this error. Any one please give me ideas.
Posted
Comments
Manoj Sawant 15-Feb-16 2:05am    
Debug your code and check what value you get in "row.Cells["paid"].Value"
Boopalslm 15-Feb-16 2:10am    
"paid" is my database table column name
CPallini 15-Feb-16 2:28am    
OK, but you should check, at runtime, using the debugger the actual value of row.Cells["paid"].Value.
Sinisa Hajnal 15-Feb-16 3:14am    
My guess would be that paid is not null, but is not a number...what is the column type of paid? If it is not numeric format, why not? If it is, check that database and your code process use same number format, you may have problems with decimal point and thousand separator mix-up.
F-ES Sitecore 15-Feb-16 10:47am    
Try

total += double.Parse(row.Cells["paid"].Value);

1 solution

This line of code is the issue:
C#
total += (double)row.Cells["paid"].Value;


The error says you cannot convert to double whatever is in row.Cells["paid"].Value. Perhaps it is a blank. Regardless, debug the code and you'll see.
 
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