Winforms datagridview calculated field change event

I have a datagridview related to datatable. I have a calculated columnar linetotal that multiplies the number of units.

I also have a totalprice label that I would like to contain the sum of the linetotal column in a datagridview.

When the user makes any changes to the price or number of units, linetotal should update for this row, and the totalprice label should summarize the Linetotal column.

I cannot find a suitable event to calculate the totalprice metric. I tried cellvaluechanged, currentcelldirtystatechanged, rowleave. None of them seem to be working correctly. I think I need an event that fires after the Linetotal columns are computed.

My pseudo code:

dt = getallitems(itemnumber); dt.Columns.Add("LineTotal", typeof(double)); dt.Columns["Linetotal"].Expression = "[unitprice] * [quantity]"; dgv.DataSource = dt; private void dgv_WhatEventToUse???(object sender, DataGridViewCellEventArgs e) { //method to iterate the rows of the datagridview, and sum linetotal column } 

There must be some kind of event that fires when the value of the calculated column changes.

+4
source share
3 answers

Your problem seems to be related to the fact that your computed column is part of the DataTable and not part of the DataGridView. I'm not sure if the DataGridView provides an event for whenever the original dgv data source changes. There is a DataGridView.DataSourceChanged event, but this only fires when you actually set the DataSource property, and not when the DataTable changes itself to your calculations.

If you want it to work without changing the structure of your code, subscribe to the DataTable RowChanged event, and you can scan dgv and update the shortcut from there. This is not a completely clean solution, but it works. I would prefer to switch to a computed column in the DataGridView if this is an option.

+1
source

Every time you need to update the calculated data column, just use the expression assignment again. This works for me.

 dt.Columns["Linetotal"].Expression = "[unitprice] * [quantity]" 

verbi gratia in my code

 private void malla18_Mask_Validated(object sender, EventArgs e) { analisis_Tabla_Lote.Columns["SUMA_MALLAS"].Expression = "100 - (Malla12 + Malla14 + Malla15 + Malla16 + Malla17 + Malla18 )"; analisis_BindingSource.ResetBindings(false); } 

That is all you need!

+1
source

I usually test my answers before giving them, but I am leaving my dev computer, so I'm not 100% sure which route is better, but here are some ideas you can try.

In the DataGridView, you can use the CellEndEdit event to indicate when the cell was edited. there is also a CellLeave event, but I have not used it, and you may need to experiment with it.

In a DataGrid, you can use the CurrentCellChanged event.

However, in order to do any calculations, you will need to extract your values ​​from the underlying Dataable or DataView (depending on what the control is associated with). I also found that using the DataGrid.CurrentCellChanged event is an error, and it is thrown when the DataGrid is databaound, and not just when the user is editing the content, so you might have to deal with it ... I would suggest that the DataGridView CellLeave event may have similar problems. As I said, I'm not 100% sure, so you need to experiment.

In addition, as Kyle suggested, you can try to use events from the underlying DataSource.

DataTable.RowChanged or DataView.ListChanged events can be triggered.

As I said, I can’t test them myself, but I hope one of these ideas will point you in the right direction.

0
source

All Articles