Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to handle the event <code>CellValueChanged</code>. However your approach is a little bad because each time a cell's value changes, you have to loop through all the rows to update the sum. You should <strong>track</strong> your cell change to store the last value, then you can update the sum easily. The sum needs to be calculated initially using the loop. It's just done 1 time.</p> <pre><code>//This is used as CellTemplate for your interested column public class TrackedValueDataGridViewCell : DataGridViewTextBoxCell { public object OldValue { get; set; } protected override bool SetValue(int rowIndex, object value) { OldValue = Value; return base.SetValue(rowIndex, value); } } public partial class Form1 : Form { public Form1(){ InitializeComponent(); //init your grid dataGridView1.DataSource = yourDataSource; dataGridView1.Columns["sumColumn"].CellTemplate = new TrackedValueDataGridViewCell(); sum = InitSum(dataGridView1,"sumColumn"); textBox9.Text = sum.ToString(); dataGridView1.CellValueChanged += (s,e) =&gt; { if(dataGridView1.Columns[e.ColumnIndex].Name != "sumColumn") return; var cell = ((TrackedValueDataGridViewCell) dataGridView1[e.ColumnIndex, e.RowIndex]); sum -= ((double?) cell.OldValue).GetValueOrDefault(); sum += ((double?)cell.Value).GetValueOrDefault(); textBox9.Text = sum.ToString(); }; } double sum; public double InitSum(DataGridView grid, string colName) { return grid.Rows.OfType&lt;DataGridViewRow&gt;() .Sum(row =&gt; ((double?) row.Cells[colName].Value).GetValueOrDefault()); } } </code></pre> <p><strong>NOTE</strong>: I suppose the column you want to sum up is named <code>sumColumn</code>, we should use <code>Name</code> to reference column because it's more readable. The code above doesn't count the cases when adding new rows and removing existing rows from grid. In fact, whenever adding and removing programmatically, you should update the <code>sum</code> easily right before adding and removing. For adding we can handle the <code>RowsAdded</code> event. All the following event handler registrations should be placed in some <code>Form.Load</code> event handler:</p> <pre><code>dataGridView1.RowsAdded += (s, e) =&gt; { for (int i = e.RowIndex; i &lt; e.RowIndex + e.RowCount; i++) { sum += ((double?)dataGridView1["sumColumn", i].Value).GetValueOrDefault(); } textBox9.Text = sum.ToString(); }; </code></pre> <p>However for removing rows, it's very tricky. The <code>RowsRemoved</code> doesn't work well, we can't refer the removed rows anymore in the <code>RowsRemoved</code> event handler, so we may have to loop through all the rows and update the sum:</p> <pre><code>dataGridView1.RowsRemoved += (s, e) =&gt; { sum = InitSum(dataGridView1,"sumColumn"); textBox9.Text = sum.ToString(); }; </code></pre> <p>That's for removing rows by both code and user. Another option which is better is you have to handle the <code>UserDeletingRow</code> (but this works only for removing rows by user, not by code). To handle removing rows by code, I think you can always insert code updating the <code>sum</code> before any code removing the rows, here it is:</p> <pre><code>//for user removing rows dataGridView1.UserDeletingRow += (s, e) =&gt; { sum -= ((double?) e.Row.Cells["sumColumn"].Value).GetValueOrDefault(); textBox9.Text = sum.ToString(); }; //for code removing rows, use some method to remove rows like this: public void RemoveRows(DataGridView grid, int startIndex, int count){ for(int i = startIndex; i &lt;= startIndex + count; i++){ //update the sum first sum -= ((double?)grid.Rows[i].Cells["sumColumn"].Value).GetValueOrDefault(); //then remove the row grid.Rows.RemoveAt(startIndex); } textBox9.Text = sum.ToString(); } </code></pre> <p>Note that <strong>all the event handlers</strong> should be registered <strong>after</strong> your grid has been initialized with some initial data following by the <code>sum</code> initialization. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload