Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code


C#
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
Updated 19-Jan-12 20:33pm
v2

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
 
Share this answer
 
Comments
sushilkumarsingh0 20-Jan-12 2:39am    
sushil singh
Perhaps a small change like:
C#
txttotalsum.Text = sum >= 0 ? sum.ToString() + "cr" : Math.Abs(sum).ToString() + "dr";
 
Share this answer
 
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.
C#
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
 
Share this answer
 
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:
C#
txttotalsum.Text =
    (sum == 0) ?
        "0.00 (zero balance)":
    (sum < 0) ?
        Math.Abs(sum).ToString("F3") + "cr":
        sum.ToString("F2") + "dr";
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900