Using formula in DataGridView
-
Hi, I have an Access Data table with three fields (date, tippingB, and precip). I am using dataGridView to view this table and make changes. The third field in the table (precip) is dependent on second field (tippingB). I was wondering if there is a way to use a formula in the field so that when I put a number in the second field, third field automatically gets updated. The last step of the code is updating the access table in the access database (that works so far). So, my question is if one could use a formula for a column in a dataGridView and also bind it to the dataTable so that when update command is used, the database gets updated. Thanks, Amanjot
-
Hi, I have an Access Data table with three fields (date, tippingB, and precip). I am using dataGridView to view this table and make changes. The third field in the table (precip) is dependent on second field (tippingB). I was wondering if there is a way to use a formula in the field so that when I put a number in the second field, third field automatically gets updated. The last step of the code is updating the access table in the access database (that works so far). So, my question is if one could use a formula for a column in a dataGridView and also bind it to the dataTable so that when update command is used, the database gets updated. Thanks, Amanjot
The DGV is not Excel. There are no formula you can put into a cell. Your code has to handle the CellValueChanged[^] event. You can then get the row and cell that changed from the event args, do your calculation and update the appropriate cell.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi, I have an Access Data table with three fields (date, tippingB, and precip). I am using dataGridView to view this table and make changes. The third field in the table (precip) is dependent on second field (tippingB). I was wondering if there is a way to use a formula in the field so that when I put a number in the second field, third field automatically gets updated. The last step of the code is updating the access table in the access database (that works so far). So, my question is if one could use a formula for a column in a dataGridView and also bind it to the dataTable so that when update command is used, the database gets updated. Thanks, Amanjot
The answer given by Dave Kreskowiak is absolutely correct. An alternative is to use
Expression
property ofprecip
DataColumn
object to calculate the value based on the value of thetippingB DataColumn
as explained here DataColumn.Expression Property[^] After binding theDataTable
to theDataGridView
the calculated value in theprecip
column will be displayed in the DataGridView.