0

enter image description here

I am using datagridview. How to get Sum on qty in Realtime. I tried this:

DataTable Dt = (DataTable)dataGridView1.DataSource;

if (int.TryParse(Dt.Compute("SUM(Qty)", "").ToString(), out Sum))
{
    textBoxQtyinStock.Text = Sum.ToString("N0");
}

Called this function in RowLeave, CellLeave, CellValueChanged, CellValidated events of datagridview. But it doesn't work for me. How can I get this done?

Adarsh Ravi
  • 893
  • 1
  • 16
  • 39
Manu Varghese
  • 791
  • 8
  • 25
  • Have you considered using the `DataGridView`s `CellValueChanged` event? This event fires anytime a value in a cell is changed. Simply check to see if the cell changed is in the “Qty” column, and, if it is… then update the text box. Just a thought. – JohnG Apr 13 '17 at 07:08

2 Answers2

0

The approach you have to compute the sun of Qty column looks to be correct however do you see in the debugger what Dt.Compute returns?

// Declare an "object"
object sumQty;
sumQty = table.Compute("Sum(Qty)", "");

As far as i know you do not need to trigger any events to compute the data you already have in the DataTable.

If your question is how can i trigger an update of DataTable after new data is inserted in it - it is the EndEdit event that you need to handle for the cell being edited.

I hope it makes sense.

Digvijay
  • 774
  • 5
  • 10
  • it computes sum but not in real time. I want to calculate sum immediately after user enter a stock – Manu Varghese Apr 13 '17 at 04:24
  • Alright, then you just need to ensure that you handle the EndEdit event or any event that is triggered after new data in available in the DataTable and call table.Compute("Sum(Qty)", ""); again. – Digvijay Apr 13 '17 at 04:33
  • Can u suggest a most preferable event? – Manu Varghese Apr 13 '17 at 04:38
  • Alright sure - however you need to tell how are you binding data to the data grid. The screenshot does not say much actually! – Digvijay Apr 13 '17 at 04:58
  • I missed out the exact question, you are looking for the correct event, check http://stackoverflow.com/questions/19537784/datagridview-event-to-catch-when-cell-value-has-been-changed-by-user – Mrinal Kamboj Apr 13 '17 at 05:27
0

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();
}
JohnG
  • 9,259
  • 2
  • 20
  • 29