SUM OF COLUMNS IN DATAGRID

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
tomastoteles
Posts: 15
Joined: Mon Jan 12, 2015 7:07 am

SUM OF COLUMNS IN DATAGRID

Post by tomastoteles » Wed Mar 18, 2015 3:56 pm

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

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: SUM OF COLUMNS IN DATAGRID

Post by ghettocottage » Wed Mar 18, 2015 5:30 pm

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:

Code: Select all

SELECT Costo1, Costo2, Costo1 + Costo2 AS resultado FROM yourdatabase

jand that will return an extra column with the two added

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: SUM OF COLUMNS IN DATAGRID

Post by Klaus » Wed Mar 18, 2015 7:55 pm

HI tomastoteles,

1. welcome to the forum! :D

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
Tested and works! :D


Best

Klaus

tomastoteles
Posts: 15
Joined: Mon Jan 12, 2015 7:07 am

Re: SUM OF COLUMNS IN DATAGRID

Post by tomastoteles » Thu Mar 19, 2015 12:32 am

Yes friend, but I need to perform operations at runtime and then run an update based on what has changed in the datagridview.

tomastoteles
Posts: 15
Joined: Mon Jan 12, 2015 7:07 am

Re: SUM OF COLUMNS IN DATAGRID

Post by tomastoteles » Thu Mar 19, 2015 12:35 am

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 !!!

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: SUM OF COLUMNS IN DATAGRID

Post by Klaus » Thu Mar 19, 2015 1:35 am

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

tomastoteles
Posts: 15
Joined: Mon Jan 12, 2015 7:07 am

Re: SUM OF COLUMNS IN DATAGRID

Post by tomastoteles » Thu Mar 19, 2015 2:24 pm

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:

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!

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: SUM OF COLUMNS IN DATAGRID

Post by Klaus » Thu Mar 19, 2015 7:11 pm

Hola tomas,
I need your help please!
please stop saying this! This is the MEANING of a forum, that users help other users 8)

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

tomastoteles
Posts: 15
Joined: Mon Jan 12, 2015 7:07 am

Re: SUM OF COLUMNS IN DATAGRID

Post by tomastoteles » Thu Mar 19, 2015 10:20 pm

Sorry, you're right.

And yes, I want to do exactly, and indeed, is within the event "mouseup"

Thanks!

:D

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: SUM OF COLUMNS IN DATAGRID

Post by Klaus » Fri Mar 20, 2015 1:39 pm

Hola Tomas,

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
...
Best

Klaus

tomastoteles
Posts: 15
Joined: Mon Jan 12, 2015 7:07 am

Re: SUM OF COLUMNS IN DATAGRID

Post by tomastoteles » Fri Mar 20, 2015 3:53 pm

I managed to solve the problem

THANKS VERY MUCH!!!!!!!!

That was what I wanted !!!

We contacted community! regards

:D :D :D :D :D :D :D :D :D :D :D

Post Reply

Return to “Databases”