Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
try
           {
               LblDinarKarton.Text =
                       (from DataGridViewRow row in dataGridView6.Rows
                        where Convert.ToDouble( row.Cells[10].FormattedValue.ToString().Replace(",","")) != 0
                        select (Convert.ToDouble(row.Cells[10].FormattedValue.ToString().Replace(",", "")) * Convert.ToDouble(row.Cells[2].FormattedValue.ToString().Replace(",", ""))) + ((Convert.ToDouble(row.Cells[10].FormattedValue.ToString().Replace(",", "")) / Convert.ToDouble(row.Cells[3].FormattedValue.ToString().Replace(",", ""))) * (Convert.ToDouble(row.Cells[4].FormattedValue.ToString().Replace(",", ""))))).Sum().ToString();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }


What I have tried:

C#
try
           {
               LblDinarKarton.Text =
                       (from DataGridViewRow row in dataGridView6.Rows
                        where Convert.ToDouble( row.Cells[10].FormattedValue.ToString().Replace(",","")) != 0
                        select (Convert.ToDouble(row.Cells[10].FormattedValue.ToString().Replace(",", "")) * Convert.ToDouble(row.Cells[2].FormattedValue.ToString().Replace(",", ""))) + ((Convert.ToDouble(row.Cells[10].FormattedValue.ToString().Replace(",", "")) / Convert.ToDouble(row.Cells[3].FormattedValue.ToString().Replace(",", ""))) * (Convert.ToDouble(row.Cells[4].FormattedValue.ToString().Replace(",", ""))))).Sum().ToString();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
Posted
Updated 19-Feb-20 23:44pm

1 solution

Given all that spaghetti code, I am not surprised. Don't use single statements with so many compound parts like that. And don't use Convert.ToDouble, especially in compound statements, as it is impossible to find problems if the string is not valid. Use Double.TryParse so you can catch errors in the text.

And if you want to know why the string has an error then use your debugger to see what it contains. As it stands no one can possibly guess what it is.
 
Share this answer
 
Comments
Maciej Los 20-Feb-20 6:49am    
5ed!

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