Click here to Skip to main content
15,885,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friend,i am facing an issue that I want to show double values and I want to show that values in text box or message box I have sum values but I don't know how to show in message box when I use this query message box.show(sum1.tostring());so it returns null values please guide me how can i show this sum values in message box.Thanks

What I have tried:

Double sum1 = 0;
           //int rowIdx1 = dataGridView2.Rows.Count;
           for (int i = 0; i < dataGridView2.Rows.Count; ++i)
           {
               sum1 = Double.Parse(dataGridView2.Rows[i].Cells[1].Value.ToString());
           }
         message box.show(sum1.tostring());
Posted
Updated 5-Mar-17 6:42am
Comments
[no name] 5-Mar-17 7:45am    
Actually, it does not return anything as your code would not compile. Then once you get the compilation errors fixed so your code will run, learn how to use the debugger to find and fix your logic errors.
Member 9983063 5-Mar-17 7:50am    
there is no error
[no name] 5-Mar-17 7:55am    
Yes there is. I can see it plain as day. Your code will not compile on anyone's computer so it will not run at all.
[no name] 5-Mar-17 7:55am    
Sum? At the Moment it Looks like the last value.
How is about sum1+= ...
Or maybe I don't get the Point.

This will take care of empty/null values in the cells

Double sum = 0;          
            for (int i = 0; i < dataGridView2.Rows.Count; ++i)
            {
                double value;
                Double.TryParse(dataGridView2.Rows[i].Cells[1].Value.ToString(),out value);
                sum += value;
            }
            MessageBox.Show(sum.ToString());
 
Share this answer
 
Shouldn't it be
sum1 += Double.Parse(dataGridView2.Rows[i].Cells[1].Value);
In fact, you should use Double.TryParse Method (String, Double) (System)[^]
 
Share this answer
 
v2
It seems you are using SQL... so use SQL's power and sum the value at the SQL level...
SUM (Transact-SQL)[^]
SQL
SELECT SUM(double_colum) FROM table1

But if you insist to do it in code, do it right...
C#
Double sum1 = 0;
for (int i = 0; i < dataGridView2.Rows.Count; ++i)
{
  // this line is all wrong:
  // 1. Why to convert back and forth your data - it is a number in SQL it will be a number here too!!!
  // 2. In every iteration you put the last value in sum1 instead of adding it... use += instead of = !!!
  sum1 = Double.Parse(dataGridView2.Rows[i].Cells[1].Value.ToString());
}
message box.show(sum1.tostring());

A suggestion to do it better...
C#
double sum = 0
foreach (DataRow oDataRow in dataGridView2.Rows)
{
  sum += oDataRow.Cells[1].Value;
}
 
Share this answer
 
IF you want the simple format as an alternative to the above answers:
C#
sum1 = sum1 + Double.Parse(dataGridView2.Rows[i].Cells[1].Value);
which is exactly the same as
C#
sum1 += Double.Parse(dataGridView2.Rows[i].Cells[1].Value);
 
Share this answer
 
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
C#
Double sum1 = 0;
            //int rowIdx1 = dataGridView2.Rows.Count;
            for (int i = 0; i < dataGridView2.Rows.Count; ++i)
            {
                sum1 += Double.Parse(dataGridView2.Rows[i].Cells[1].Value.ToString());
            }
          message box.show(sum1.tostring());


[edit]: added in code block.
 
Share this answer
 
v3

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