SUM OF COLUMNS IN DATAGRID
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 15
- Joined: Mon Jan 12, 2015 7:07 am
SUM OF COLUMNS IN DATAGRID
Hello. I have a Database and I show it in a DataGrdi. I have been looking for information and I have not found. How could add the value of each column ?. I want each column in each row add up and finally I go putting the result in another. example:
[COLUMN]
Costo1___Costo2____RESULTADO
__2_______2__________4
__3_______3__________6
__ 0 _______0__________0
THANKS
[COLUMN]
Costo1___Costo2____RESULTADO
__2_______2__________4
__3_______3__________6
__ 0 _______0__________0
THANKS
-
- Livecode Opensource Backer
- Posts: 366
- Joined: Tue Apr 10, 2012 9:18 am
Re: SUM OF COLUMNS IN DATAGRID
what type of database are you using?
I ask because if you are sending a query to your SQL database, you can probably do something like this:
jand that will return an extra column with the two added
I ask because if you are sending a query to your SQL database, you can probably do something like this:
Code: Select all
SELECT Costo1, Costo2, Costo1 + Costo2 AS resultado FROM yourdatabase
jand that will return an extra column with the two added
Re: SUM OF COLUMNS IN DATAGRID
HI tomastoteles,
1. welcome to the forum!
2. You will need to loop through all your data from the database and compute the sum.
Do like this, I presume you have your data in a variable named tData adn are a TAB and CR sepearated list:
Tested and works!
Best
Klaus
1. welcome to the forum!
2. You will need to loop through all your data from the database and compute the sum.
Do like this, I presume you have your data in a variable named tData adn are a TAB and CR sepearated list:
Code: Select all
## Columns are separated by TAB, so we use this as itemdelimiter
set itemdel to TAB
## We will create a new list of data with the sum as the last TAB delimited item in each line/row!
put empty into tNewdata
## We process each line of your db data:
repeat for each line tLine in tData
## Start anew in every repeat and add all columns
put 0 into tSum
## We process each ITEM (column) in each line of columns
repeat for each item tItem in tLine
add tItem to tSum
end repeat
## #now we create new data with the SUM as the last item for each line
put tLine & TAB & tSum & CR after tNewData
end repeat
## Delete trailing CR:
delete char -1 of tNewData
## Now display data in datagrid
set the dgtext of grp "your datagrid here..." to tNewData
Best
Klaus
-
- Posts: 15
- Joined: Mon Jan 12, 2015 7:07 am
Re: SUM OF COLUMNS IN DATAGRID
Yes friend, but I need to perform operations at runtime and then run an update based on what has changed in the datagridview.
-
- Posts: 15
- Joined: Mon Jan 12, 2015 7:07 am
Re: SUM OF COLUMNS IN DATAGRID
First of all thank you very much for answering friend KLAUS! = D ... I tried a thousand ways and could not get to do the operation I want (The one that showed up) .. I'm really learning the use of datagrid and I was a bit complicated to understand what the code because I do not see where the rows sum indicating. Could I help a little friend? Thank you very much !!!
Re: SUM OF COLUMNS IN DATAGRID
Hi tomastoteles,
sorry, I don't understand your problem?
Do you have an existing datagrid and need to calculate the sum for each row?
Of course this will require that your datagrid already have a column for the sum,
if that is what you mean?
Best
Klaus
sorry, I don't understand your problem?
Do you have an existing datagrid and need to calculate the sum for each row?
Of course this will require that your datagrid already have a column for the sum,
if that is what you mean?
Best
Klaus
-
- Posts: 15
- Joined: Mon Jan 12, 2015 7:07 am
Re: SUM OF COLUMNS IN DATAGRID
i dear KLAUS. I'm starting in the realization of a project, where I choked on operations with a datagrid. The operations will need to do in real time as they simulate some sales. See friend, I have the following function that shows me the data:
In this logic, what I do is that changing the values from the second datagrid in COL 1, and that they add to the values of COL 1 of the first datagrid, and that the result of that it put me in the COL 3 of the first data grid. According to the image and a hypothetical example would be something like:
I need your help please!
Sorry for the inconvenience friend. I think it's easy to do, but I just begin to handle the datagrid. Best Regards!
Code: Select all
global ConID
on mouseUp
conexionOperacional
# Consulta en el Datagrid
put "SELECT * FROM prueba" into tSQL
put "Select VENTAS FROM temporales" into tSQL2
put revDataFromQuery(tab,cr,ConID,tSQL) into tRecords
put revDataFromQuery(tab,cr,ConID,tSQL2) into tRecords2
# Checa Errores
if tRecords begins with "revdberr" then
answer error "Hubo un problema con los datos de la Base de Datos" & tRecords
closeDB ConID
exit to top
end if
# Termina chequeo
//closeDB ConID
set the dgText of group "Datos_Alumnos" to tRecords
set the dgText of grp "Ventas" to tRecords2
end mouseup
In this logic, what I do is that changing the values from the second datagrid in COL 1, and that they add to the values of COL 1 of the first datagrid, and that the result of that it put me in the COL 3 of the first data grid. According to the image and a hypothetical example would be something like:
I need your help please!
Sorry for the inconvenience friend. I think it's easy to do, but I just begin to handle the datagrid. Best Regards!
Re: SUM OF COLUMNS IN DATAGRID
Hola tomas,
OK, so you want to have this: (column 3 of datagrid 1) = (column 1 of dg 1) + (column 1 of dg 2)
Is that correct?
And if yes, do you want to do this right after fetching the data from the database, means in your "mouseup" handler?
Best
Klaus
please stop saying this! This is the MEANING of a forum, that users help other usersI need your help please!
OK, so you want to have this: (column 3 of datagrid 1) = (column 1 of dg 1) + (column 1 of dg 2)
Is that correct?
And if yes, do you want to do this right after fetching the data from the database, means in your "mouseup" handler?
Best
Klaus
-
- Posts: 15
- Joined: Mon Jan 12, 2015 7:07 am
Re: SUM OF COLUMNS IN DATAGRID
Sorry, you're right.
And yes, I want to do exactly, and indeed, is within the event "mouseup"
Thanks!
And yes, I want to do exactly, and indeed, is within the event "mouseup"
Thanks!
Re: SUM OF COLUMNS IN DATAGRID
Hola Tomas,
here we go, just add these lines to your "mouseup" script:
Best
Klaus
here we go, just add these lines to your "mouseup" script:
Code: Select all
...
# Termina chequeo
//closeDB ConID
## This requires that BOTH data have the same number of lines/records!
set itemdel to TAB
repeat with i = 1 to the num of lines of tRecords
put item 1 of line i of tRecords + item 1 of line i of tRecords2 into item 3 of line i of tRecords
end repeat
set the dgText of group "Datos_Alumnos" to tRecords
set the dgText of grp "Ventas" to tRecords2
...
Klaus
-
- Posts: 15
- Joined: Mon Jan 12, 2015 7:07 am
Re: SUM OF COLUMNS IN DATAGRID
I managed to solve the problem
THANKS VERY MUCH!!!!!!!!
That was what I wanted !!!
We contacted community! regards
THANKS VERY MUCH!!!!!!!!
That was what I wanted !!!
We contacted community! regards