Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 RichText boxs Summing 2 columns. The first Box Sums correctly for the column, the second box sums both columns instead of just 1 column.

Box 1 should sum column 6 and Box2 should sum column 7

Thanks for any help

C#
<pre>private void LoadDataGridView(DataTable data)
        {
            // Data grid view column setting            
            dataGridViewMembers.DataSource = data;
            dataGridViewMembers.DataMember = data.TableName;
            dataGridViewMembers.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

            // Sum Rich Text Box
            //
            //
            double sum = 0;
            for (int i = 0; i < dataGridViewMembers.Rows.Count; ++i)
            {
                sum += Convert.ToDouble(dataGridViewMembers.Rows[i].Cells[6].Value);
            }
            rtVacSum.Text = sum.ToString("N2");

            //// double sum = 0;
            for (int i = 0; i < dataGridViewMembers.Rows.Count; ++i)
            {
                sum += Convert.ToDouble(dataGridViewMembers.Rows[i].Cells[7].Value);
            }
            rtCompTime.Text = sum.ToString("N2");
        }


What I have tried:

Different property setting, inter net lookup
Posted
Updated 24-Jun-19 21:34pm
Comments
Member 12349103 24-Jun-19 21:08pm    
how do I clear
[no name] 24-Jun-19 21:19pm    
sum = 0;

You forgot to clear "sum" for the next iteration.
 
Share this answer
 
Comments
Maciej Los 26-Jun-19 14:49pm    
Hawk eye!
0. assuming you bind the DGV to a DataTable:

1. you probably want to update totals in the 'CellEndEdit, or 'Parsing, or 'Validation, EventHandler

2. why not use the 'Compute function of the DataTable to dynamically update the totals in the DataGridView ?

3. see this for useful Extensions to Sum DataTable columns in a variety of ways: [^]
 
Share this answer
 

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