XBrowse : Calculated Columns

XBrowse : Calculated Columns

Postby nageswaragunupudi » Thu Mar 04, 2010 7:11 am

Similar to Excel, we can also create calculated columns in XBrowse. This is another less known feature of XBrowse.

Here is a small sample. Two calculated columns are created with a simple syntax like :
Code: Select all  Expand view
  oBrw:DueDate   := { || oBrw:Date:Value + oBrw:Credit:Value }
   oBrw:Interest  := { || Max( Date() - oBrw:DueDate:Value, 0 ) * oBrw:Advance:Value * 0.1 }
 

This code is easy to write and easy to understand and maintain. This code has an additional benefit of being portable across different sources of data ( RDD, Array, RecordSet, etc ).

Sample code:
Code: Select all  Expand view
#include 'fivewin.ch'
#include 'xbrowse.ch'

function Main()

   local oDlg, oBrw, oFont
   local aData

   SET DATE ITALIAN
   SET CENTURY ON

   aData := CreateRandomData()

   DEFINE FONT oFont NAME 'TAHOMA' SIZE 0,-12
   DEFINE DIALOG oDlg SIZE 500,400 PIXEL FONT oFont ;
      TITLE 'XBrwose Calculated Columns'

   @ 10,10 XBROWSE oBrw SIZE -10,-10 PIXEL OF oDlg ;
      AUTOCOLS ARRAY aData ;
      HEADERS 'Date', 'Advance', 'Credit' ;
      PICTURES nil, '9,999,999.99', '99 Days' ;
      CELL LINES NOBORDER FASTEDIT

   AEval( oBrw:aCols, { |o| o:nEditType := EDIT_GET } )

   // Now add calculated columns

   oBrw:DueDate   := { || oBrw:Date:Value + oBrw:Credit:Value }
   oBrw:Interest  := { || Max( Date() - oBrw:DueDate:Value, 0 ) * oBrw:Advance:Value * 0.1 }

   // end of Calculated columns

   oBrw:DueDate:bClrStd       := { || { If( oBrw:DueDate:Value < Date(), CLR_HRED, CLR_BLACK ), CLR_WHITE } }
   oBrw:Interest:cEditPicture := '@Z 9,999,999.99'
   oBrw:nStretchCol           := 2

   oBrw:CreateFromCode()
   ACTIVATE DIALOG oDlg CENTERED
   RELEASE FONT oFont

return nil

static function CreateRandomData()

   local aData := {}
   local n

   for n := 1 to 50
      AAdd( aData, ;
      {  Date() - HB_RandomInt( 90 ), ;
         Round( HB_Random( 5000, 25000 ), 2 ), ;
         HB_RandomInt( 1, 3 ) * 15 } )
   next n

return aData
 


ScreenShot:
Image

First three columns can be edited and the calculated columns ( DUEDATE and INTEREST) are recalculated and refreshed automatically.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10627
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: XBrowse : Calculated Columns

Postby anserkk » Thu Mar 04, 2010 7:22 am

Dear Mr.Rao,

Thank you for providing very useful samples which shows the power of xBrowse. May be many of us here do not know about these powerful features of xBrowse.

Regards
Anser
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 90 guests