Click here to Skip to main content
14,328,292 members
Rate this:
Please Sign up or sign in to 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.
Wes Aday 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
Wes Aday 26-Jul-15 7:18am
   
Okay so now post the code that you have written and describe the problem with your code.
Jaybo007 27-Jul-15 18:58pm
   
Sorry Wes, The problem is where to start. I have no idea. I have generously been shown 1 idea which has given me somewhere to start, but I am having issues implementing it.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
Dim totalColumn As DataColumn = New DataColumn
With totalColumn
    .DataType = System.Type.GetType("System.Int32")
    .ColumnName = "Total"
    .Expression = "TempoFinish-TempoStart"
End With
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100