The DataGridView
’s CellValueChanged
event appears to be what you may be looking for. This event fires every time a cells value changes. When this event fires, a check is made to see if the cell changed was in the column that contains the values we want to sum. If the changed cell IS in the column we want to sum, then simply loop through the values in the column and update the textbox to reflect this new summed value.
Below is an example of what I am describing. The 4th column (called ‘Points’) contains integer values in the column. The CellValueChanged
event checks to see if the cell changed is from the “Points” column (column 3) and if it is, then updates the text box.
When users enter new values or change existing values… the text box should automatically update to reflect these changes. I hope this makes sense and works for you.
DataTable dt = new DataTable();
private void Form_DGV_DataTable_Load(object sender, EventArgs e) {
dt = Utilities.GetTableWithPlayers(filePath);
dataGridView1.DataSource = dt;
UpdatePoints();
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex == 3)
UpdatePoints();
}
private void UpdatePoints() {
textBox1.Text = dt.Compute("SUM(Points)", "").ToString();
}