Click here to Skip to main content
14,298,919 members
Rate this:
Please Sign up or sign in to vote.
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

<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
Gerry Schmitz 24-Jun-19 21:19pm
   
sum = 0;
Rate this:
Please Sign up or sign in to vote.

Solution 1

You forgot to clear "sum" for the next iteration.
   
Comments
Maciej Los 26-Jun-19 14:49pm
   
Hawk eye!
Rate this:
Please Sign up or sign in to vote.

Solution 2

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: [^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100