Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code below is not summing the values under datagridview column[4] as rows are being added to the datagridview (saleslist) . My code is as follows:

C#
private void saleslist_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 4)
            total.Text = CellSum().ToString();
    }

    private double CellSum()
    {
        double sum = 0;
        for (int i = 0; i < saleslist.Rows.Count; ++i)
        {
            double d = 0;
            Double.TryParse(saleslist.Rows[i].Cells[4].Value.ToString(), out d);
            sum += d;
        }
        return sum;
    }
Posted
Comments
Sergey Alexandrovich Kryukov 1-Sep-15 19:07pm    
Listen to a good advice: do your calculation on data, not control; bind data to control using binding.
Also, use the debugger. And don't hard-code "magic numbers" line 4. Define explicit constants and calculate everything.
—SA
Member 10908573 2-Sep-15 5:47am    
What do you mean? I do not understand when you say 'do your calculation on data, not control'
Sergey Alexandrovich Kryukov 2-Sep-15 8:45am    
Read on this topic, "data binding". Just a suggestion.
—SA

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


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