Hi Friends
i have a datagird and it has 2 columns and have calculate with the content of them but when i typed them in runtime my result was wrong i followed it by BreakPoint and underestood that the content of last column didn't use in my calculate(although it has some content) what's its reason? why it happened? how i can solve it?

Thank you

Recommended Answers

All 12 Replies

Can you post the code that is not working properly? It is probably a good idea to post all relevant code for the entire class.

and this is my code:
the last column is Creditor but the value of it is not involved in Calculation

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                long DebtSum = 0;
                long CredSum = 0;
                long Remain = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Cells[2].Value != null | dataGridView1.Rows[i].Cells[3].Value != null)
                    {
                        DebtSum = DebtSum + Convert.ToInt64(dataGridView1.Rows[i].Cells[2].Value);
                        CredSum = CredSum + Convert.ToInt64(dataGridView1.Rows[i].Cells[3].Value);
                    }
                }

                if (DebtSum > CredSum)
                    Remain = DebtSum - CredSum;
                else
                    Remain = CredSum - DebtSum;

                MessageBox.Show(Remain.ToString());
            }
            catch
            { }
        }

Try it with your datasource object (DataTable) and also expression columns. For example,

DataTable dt=new DataTable();
            dt.Columns.Add("Qty",typeof(int));
            dt.Columns.Add("Rate", typeof(int));
            dt.Columns.Add("Amount", typeof(int), "Qty*Rate");
            dt.Rows.Add(2, 2);
            dt.Rows.Add(2, 3);
            dt.Rows.Add(6, 5);
            int total=0;
            foreach(DataRow  v in  dt.Rows){
                total = total + (int) v[2];
            }
           ....
commented: working at the data level is always the best! +9

write these code on RowLeave also?

I would do what adatapost suggested. Also the | operator does not short circuit. You should use || to benefit from the performance gain of short circuiting:

if (dataGridView1.Rows[i].Cells[2].Value != null | dataGridView1.Rows[i].Cells[3].Value != null)

To:

if (dataGridView1.Rows[i].Cells[2].Value != null || dataGridView1.Rows[i].Cells[3].Value != null)

Thank you i'm waiting for your answer

I'm waiting yet!!!!

commented: Don't spam. Why are you waiting? -2

I'm still thinking. Give me a few more days.

Hi Friends
i have a datagird and it has 2 columns and have calculate with the content of them but when i typed them in runtime my result was wrong i followed it by BreakPoint and underestood that the content of last column didn't use in my calculate(although it has some content) what's its reason? why it happened? how i can solve it?

Thank you

You are using long integers to accumulate and calculate your remaining result--was that intended? Seems to me you would want to use the decimal type since this pertains to monetary verbiage (credit/debt).

Not sure how new you are to C#, but I will assume you know that cells[2] is actually column 3 viewable in your grid, etc.

Thank you never mind i'll get you some more times to think about it but i want you know it's to important for me
yes i'm new in C# but it's not a real problem that i said i have 2 columns i make a mistake,sorry

Hello
I don't like to Hurry but my time is going to finish and i'm worry about it
Are you still thinking about it? it's too nice

Last row of grid is for adding a new item (row),

for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
            {
                try
                {
                    n = n + int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
                }
                catch (Exception ex) { }
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.