Auto Calculation with Grid

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
sudip
Posts: 1456
Joined: Sat Mar 07, 2009 11:52 am
Location: Kolkata, WB, India

Auto Calculation with Grid

Post by sudip »

Hello All,

It was my long term query, what event should be used with AutoCalculation. I tried with ColumValid event, but was not successful. Then I tried with LostFocus event of Grid ( :o ) and it works ( :o :o ). But, I have some doubt. Yesterday, I posted this for help in this forum but later deleted, as I thought I found the answer ( ;) ), and not to disturb you again. I posted the same query into HMG Extended group, (which fortunately I couldn't delete). Grigory Fiatov advised me to use ColumnValid event and told me about one small sample. But, I thought I already able to AutoCalculate with LostFocus event of Grid (how fool I am!!!). But, today I found it didn't work when I used a ComboBox (of Products) to get the Product Rate, Cat No ect., it didn't work. Then I tried with ColumnValid event and it works fine. :) Thanks a lot Grigory :)
InfoBill.jpg
InfoBill.jpg (23.85 KiB) Viewed 1526 times
I am telling my experience in details, for the novice users, who may do the same mistake I did.
Here is the code for those who are interested.

Code: Select all

// called from ColumnValid event   
Static Function CalcAmt()
   local i, c, mIctr, mQty, mRate, mRateper, mAmt 

   if !(lAddmode .or. lEditmode)
      return nil
   endif
   i := this.CellRowIndex
   if i < 1
      return nil
   endif
   c := this.CellColIndex
   mIctr := if(c=1, this.CellValue, frmSale.grdSaledtl.cell(i, 1))
   if c = 1 .and. mIctr > 0
      frmSale.grdSaledtl.Cell(i, 2) := aProd[mIctr, 3]
      frmSale.grdSaledtl.Cell(i, 7) := aProd[mIctr, 4]
      frmSale.grdSaledtl.Cell(i, 8) := aProd[mIctr, 5]
      frmSale.grdSaledtl.Cell(i, 9) := aProd[mIctr, 6]
   endif
   mQty := if(c=6, this.CellValue, frmSale.grdSaledtl.Cell(i, 6))
   mRate := if(c=7, this.CellValue, frmSale.grdSaledtl.Cell(i, 7))
   if c = 8
      this.CellValue := if(this.CellValue <= 0, 1, this.CellValue)
   endif
   mRatePer:= if(c=8, this.CellValue, frmSale.grdSaledtl.Cell(i, 8))
   mAmt := mQty * mRate / if(mRatePer = 0, 1, mRatePer)
   if mAmt <> frmSale.grdSaledtl.Cell(i, 10)
      frmSale.grdSaledtl.Cell(i, 10) := mAmt
      CalcTotAmt()
   endif
   return nil
With best regards.

Sudip
With best regards,
Sudip
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Auto Calculation with Grid

Post by Rathinagiri »

Thanks a lot for sharing Sudip. It is very much useful.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
Post Reply