0

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

4
Contributors
12
Replies
13
Views
7 Years
Discussion Span
Last Post by __avd
Featured Replies
  • 1
    __avd 1,826   7 Years Ago

    Try it with your datasource object (DataTable) and also expression columns. For example, [code=c#] 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]; } .... [/code] Read More

0

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.

0

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
            { }
        }
1

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];
            }
           ....
Comments
working at the data level is always the best!
0

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)
0

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.

0

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

0

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

0

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) { }
            }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.