Subject Re: How to calulate field value in a Grid?
From Ken Mayer <dbase@nospam.goldenstag.net>
Date Fri, 30 Jul 2021 08:57:02 -0700
Newsgroups dbase.getting-started

On 7/30/2021 7:42 AM, Milind Nighojkar wrote:
> Thanks for the reply Akshat,
>
> Aging and Estimated Field both are table fields...
>
> I am not aware how to add calulated fields in grid .....

Create a field OBJECT that has the calculated and add it to the rowset.
The tricky part is adding it to the grid, because the first time you
open the form in the designer and create the field it won't exist when
you try to add it to the grid. You would want to create the calculated
field(s), add them to the rowset/fields array, and close the form. Then
open it again in the designer, and in theory they should be there.

Now, calculated fields can be added in a query object's canOpen event
handler:

// modification of sample code in the help, I changed "q.rowset.fields"
to "this.rowset.fields" because you're working in the query object's
canOpen event ...:

    c = new Field()
    c.fieldName := "Total"
    this.rowset.fields.add(c)
    c.beforeGetValue := {||this.parent["Quantity"].value *
this.parent["PricePer"].value}

The calculation would go in the beforeGetValue. Understand that "this"
in the calculation shown above is the current field object, parent is
the fields array.

Hope that helps a little.

Ken


--
*Ken Mayer*
Ken's dBASE Page: http://www.goldenstag.net/dbase
The dUFLP: http://www.goldenstag.net/dbase/index.htm#duflp
dBASE Books: http://www.goldenstag.net/dbase/Books/dBASEBooks.htm
dBASE Tutorial: http://www.goldenstag.net/dbase/Tutorial/00_Preface.htm
dBASE Web Tutorial: http://www.goldenstag.net/dbase/WebTutorial/00_Menu.htm