Click here to Skip to main content
14,266,802 members
Rate this:
Please Sign up or sign in to vote.
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
NotPolitcallyCorrect 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
NotPolitcallyCorrect 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.
0x01AA 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.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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());
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Shouldn't it be
sum1 += Double.Parse(dataGridView2.Rows[i].Cells[1].Value);
In fact, you should use Double.TryParse Method (String, Double) (System)[^]
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

It seems you are using SQL... so use SQL's power and sum the value at the SQL level...
SUM (Transact-SQL)[^]
SELECT SUM(double_colum) FROM table1

But if you insist to do it in code, do it right...
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...
double sum = 0
foreach (DataRow oDataRow in dataGridView2.Rows)
{
  sum += oDataRow.Cells[1].Value;
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

IF you want the simple format as an alternative to the above answers:
sum1 = sum1 + Double.Parse(dataGridView2.Rows[i].Cells[1].Value);
which is exactly the same as
sum1 += Double.Parse(dataGridView2.Rows[i].Cells[1].Value);
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
v3

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100