Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
Here is my code
 

public void sum1()
        {
            try
            {
                string CD;
 
                int sum = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
 
                    CD = dataGridView1[1, i].Value.ToString();
 
                    if (CD == "C")
                    {
                        sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
                    }
                    else if (CD == "D")
                    {
                        sum -= Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
                    }
                }
                txttotalsum.Text = sum.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
Want to like that if if sum=-25300 than i want to show in text box like 25300.000cr
And if sum =25300 than i want to show in text box like 25300.00dr
 
Please help me thanks in advanced
Rosy
Posted 19-Jan-12 21:14pm
rosyp471
Edited 19-Jan-12 21:33pm
Kim Togo26.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have to decide how to treat sum == 0. "0cr" or "0dr".
 
txttotalsum.Text = (sum < 0)? sum.ToString()+"cr" : sum.ToString()+"dr";
for "0dr"
txttotalsum.Text = (sum <= 0)? sum.ToString()+"cr" : sum.ToString()+"dr";
for "0cr"
 

regards
Michel
  Permalink  
Comments
sushilkumarsingh0 at 20-Jan-12 2:39am
   
sushil singh
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Perhaps a small change like:
txttotalsum.Text = sum >= 0 ? sum.ToString() + "cr" : Math.Abs(sum).ToString() + "dr";
  Permalink  
v3
Comments
rosyp at 20-Jan-12 2:42am
   
THANK YOU VERY MUCH BUT IS SUM IS >0 THAN IT GIVE MINUS SIGN HOW CAN REMOVE - SIGN
Kim Togo at 20-Jan-12 3:09am
   
You can use Math.Abs(...), see updated answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

public void sum1()
{
    try
    {
        int sum = 0;
 
        foreach(Row r in dataGridView1.Rows)
        {
 
            string text = r.Cells[1].Text;
            string value = r.Cells[2].Text;
 
            if (text.Contains("C"))
            {
                sum += Convert.ToInt32(value);
            }
            else if (text.contains("D"))
            {
                sum -= Convert.ToInt32(value);
            }
        }
 
        string suffix = string.empty;
 
        if(sum>0){suffix = "cr"}
        else if(sum<0){suffix = "dr"}
 
        txttotalsum.Text = sum.ToString() + suffix;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
 
i suggest u using the foreach loop, is safier than the for loop.
about the zero-nonzero suffix you can decide it whith an if-cascade
 
in that case there are cr for positive, dr for negative and nothing for zero.
 
it depends on what you prefer
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Try this:
txttotalsum.Text =
    (sum < 0) ?
        Math.Abs(sum).ToString("F3") + "cr":
        sum.ToString("F2") + "dR";
Note that the digit after the letter "F" in the ToString parameter controls the number of trailing digits. Did you really want three trailing digits in the case of a negative balance ?
 
Do you want to handle "zero" as a special case ? Now: a result of #0.00 is going to result in a an output of "0.00dr." Think about:
txttotalsum.Text =
    (sum == 0) ?
        "0.00 (zero balance)":
    (sum < 0) ?
        Math.Abs(sum).ToString("F3") + "cr":
        sum.ToString("F2") + "dr";
  Permalink  
Comments
rosyp at 20-Jan-12 9:10am
   
thanks dear.....very good gestation .....
again thanks ( have no word for you)

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Jan 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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