Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
I am stumped and have no idea where to start with this. So any help will be greatly appreciated.

I would like to add the sum of 2 cells together and insert the result into a new cell in that same datagrid.

Something like this below:
Existing Cell 1 (Tempo Finish) = 120 - Existing Cell 2 - (Tempo Start) = 90 New Cell (Total) = 30

These cells must calculate on each row of the datagrid (not a column total).
I'm obviously trying to calculate the difference of the 2 cells to reflect a tempo improvement or decrease in speed, so the und user can instantly see the result.

I can get a result in the Access database but I'd like to show this in the datagrid without using a Access query table. I want this information applied on form load.

thanks in advance
Jason
Posted
Comments
Wendelius 26-Jul-15 1:14am    
What is the data source for the grid? Do you use a DataTable?
Jaybo007 26-Jul-15 1:52am    
The data source is an Access DB and I am using a DataTable to load the datagrid.
[no name] 26-Jul-15 1:15am    
I am not sure what your question is exactly. You start by looking at the documentation for the datagridview class and see if there is something in there that might help you with your task. Datagridview contain rows and rows contain cells. So then you might think to yourself, "Self, I might iterate through all of the rows, get the values for the cells I want, then calculate the result and put that in another cell.". Then you write some code that does that. See https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows(v=vs.110).aspx
Jaybo007 26-Jul-15 1:53am    
I'm just trying to (Minus) the contents of 2 cells and create and populate another cell with the result of the calculation. 120-90=30
I don't want this as a button click event, I need it to refresh the datagrid with the answer automatically when information is entered into the database..
Jaybo007 26-Jul-15 2:08am    
This is a link to another forum where I have placed an image of what I'm trying to achieve. It might clear things up about what I'm trying to accomplish.
http://stackoverflow.com/questions/31633807/how-do-i-sum-2-cells-and-add-the-result-into-another-cell-in-datagrid

1 solution

Since you're using a data table, I wouldn't do the calculation using the grid but the data table. This way the presentation is kept separately from the data.

A data table has the ability to contain computed columns. Have a look at DataColumn.Expression Property[^]

So the formula could be something like
VB
Dim totalColumn As DataColumn = New DataColumn
With totalColumn
    .DataType = System.Type.GetType("System.Int32")
    .ColumnName = "Total"
    .Expression = "TempoFinish-TempoStart"
End With
 
Share this answer
 
v2
Comments
Jaybo007 26-Jul-15 2:44am    
Thank you Mika. I now have somewhere to start. Can you explain System.Int32 please or send a link so I can learn about it? Thanks Again
Wendelius 26-Jul-15 3:20am    
Glad it helped :)

System.Int32 is the data type name used for the column. In VB the alias of it is Integer. In other words, whenever you define a variable of type Integer you use System.Int32 structure. For more details, have a look at https://msdn.microsoft.com/en-us/library/vstudio/system.int32(v=vs.100).aspx[^]
Jaybo007 26-Jul-15 3:23am    
Thank you so much for your guidance Mika. I owe you a beer.
Wendelius 26-Jul-15 3:37am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900