Click here to Skip to main content
12,449,149 members (54,838 online)
Rate this:
 
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 20:14pm
rosyp471
Updated 19-Jan-12 20:33pm
Kim Togo26.9K
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 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 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 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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 20 Jan 2012
Copyright © CodeProject, 1999-2016
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